1.scala中的match语句用来在一个列表中选择某一个分支来执行分支的语句块,类似于其他语言中的swtich..case语句
package smart.iot class matchCase { } object matchCase { def main(args: Array[String]): Unit = { val a: Int = 2; a match { case 1 => println("A") case 2 => println("B") case _ => println("oaher") } } }
result:
B
2.match case 中的控制语句
a match{ case x if x==1 =>println("A") case x if x==2 =>println("B") case _ =>println("other") }
resout:
B
3.match 类型判断
package smart.iot class matchCase { } object matchCase { //定义一个类型判断函数 def t(obj:Any)= obj match { case obj:String=>println("type is String") case obj:Int=>println("tpye is Int") case _=>println("tpye is other") } def main(args: Array[String]): Unit = { t("hello") t(5) } }