* 基础
~~~
val names = Array("Bizzbee","James","Scola")
val name = names(Random.nextInt(names.length))
name match {
case "Bizzbee" =>println("Bizzbee")
case "James" => println("James")
case "Scola" =>println("scola")
case _=>println("??!!")
}
~~~
* Array匹配
~~~
def greeting(array:Array[String])={
array match{
case Array(x,y)=>println("Hello"+x+":"+y)
case Array("zhangsan",_*)=>println("Hi zhangsan + other")
case _=>println("Hi:everybody")
}
}
greeting(Array("za","wangwu"))
~~~
* List
~~~
def gree(list:List[String])={
list match{
case "zhangsan"::Nil=>println("Hi:zhangsan")
case x::y::Nil=>println("Hi "+"x" +" y")
case "zhangsan"::tail =>("Hi tail")
case _=>println("Hi everyBody")
}
}
gree(List("zhangsan","lisi","123"))
~~~
* 类匹配
![](https://img.kancloud.cn/c2/06/c2063dc30567a75f255967e38a02f2da_891x155.png)
![](https://img.kancloud.cn/77/cb/77cb321ad6eb200e5f9bb692a3ac51ea_1082x223.png)