🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
* 基础 ~~~ 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)