1.scala中的match语句用来在一个列表中选择某一个分支来执行分支的语句块,类似于其他语言中的swtich..case语句
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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" ) } } } < br >< br > result :< br > B |
2.match case 中的控制语句
1 2 3 4 5 6 | a match { case x if x == 1 = >println( "A" ) case x if x == 2 = >println( "B" ) case _ = >println( "other" ) } < br > resout :< br > B |
3.match 类型判断
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 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 ) } } |