scala matchase (2

31 阅读1分钟
package matchase
/*
*match case
* 1.匹配精确值
* 2.匹配数据类型
 */
object matchase03 {
  def processValue(value:Any):Unit = {
    value match {
      case x:Int => println("Int")
      case x:String => println("String")
      case _:Int => println("other")
    }
  }

  def main(args: Array[String]): Unit = {
    val obj = 10
    processValue(obj)
  }

}
package matchase
/*
*match case
* 1.匹配精确值
* 2.匹配数据类型
* 3.匹配元素的个数
 */
object matchase03 {
 // def processValue(value:Any):Unit = {

    def processNum(value: Any): Unit = {
      value match {
        case (a, b, c) => println(s"三个元素的元组 第一个元素是${a}")
        case (x, y) => println("两个元素的元组")
        case _ => println("其他")
      }
    }

    def main(args: Array[String]): Unit = {
      processNum((1,2))
      processNum((1,2,3))
    }
}