本文最后更新于 2021-08-05 11:42:59
模式匹配
一般匹配
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| object Demo1 { def main(args: Array[String]): Unit = { val a = 10 val b = 20
val op = StdIn.readLine("运算符:")
op match { case "+"=> println(a+b) case "-"=> println(a-b) case _ => println("input error") } } }
|
匹配变量
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| object Demo2 { def main(args: Array[String]): Unit = { val a = 10
a match { case aa => println(aa) case 10 => println("cccccc")
} } }
|
返回值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| object Demo3 { def main(args: Array[String]): Unit = { val a = 10 val b = 20
val op = StdIn.readLine("运算符:")
val res = op match { case "+"=> a+b case "-"=> a-b case _ => -1 } } }
|
匹配类型
1 2 3 4 5 6 7 8 9 10 11 12
| object Demo4 { def main(args: Array[String]): Unit = { val a:Any = 10
a match { case b:Int=> println(b + 10) case b:String=> println(b + " 111") case _=> } } }
|
匹配加条件
1 2 3 4 5 6 7 8 9 10 11 12
| object Demo5 { def main(args: Array[String]): Unit = { val a:Int = 10
a match { case b:Int if b>5=> println(b + 10) case b:Int if b>10=> println(b + " 111") case _=> } } }
|
泛型匹配
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| object Demo6 { def main(args: Array[String]): Unit = { val ints:Any = Array(1,2)
ints match { case b:Array[Any] => println("Array[S]") } } }
|
Array匹配
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| object Demo7 { def main(args: Array[String]): Unit = { val ints:Any = Array(1,2)
ints match { case Array(1,_,_,) => println("匹配长度是3,第一个是1") case Array(1,a,b)=>println(s"匹配长度是3,并且a=$a") case Array(1,abc@_*)=>println("匹配第一个是1,长度不限制,* 相当于..."+abc.toList.toString()) case Array(1,_*)=>println("匹配第一个是1,长度不限制,* 相当于...") } } }
|
元组匹配
1 2 3 4 5 6 7 8 9 10
| object Demo8 { def main(args: Array[String]): Unit = { val aa = ("111",11) aa match { case (name:String,age) => println(s"$name,$age") } } }
|
List匹配
1 2 3 4 5 6 7 8 9 10 11 12 13
| object Demo9 { def main(args: Array[String]): Unit = { val strings = List("a","b","c")
strings match { case List("a","b",c,d)=> println(s"匹配a,b开头 并且长度为4 $c,$d") case List("a","b",aa@_*)=> println(s"匹配a,b开头 并且长度不限制 $aa") case a::b::c::d::Nbil=>pbrintln("匹配长度为4的集合") case a::b=> println("b变量相当于是一个集合"+b.toString()) } } }
|
Option匹配
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 26 27 28 29 30
| class People(val age:Int,val name:String) object People{
def unapply(arg: People): Option[(Int, String)] ={ if(arg==null){ None }else{ Some(arg.age,arg.name) } }
}
object Demo10 { def main(args: Array[String]): Unit = { val strings = new People(1,"11")
strings match {
case People(age,name)=> println(s"解析people即调用unapply $age,$name") case _=> println("---") } } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| object MyArray { def unapplySeq(s:String)={ if(s==null){ None }else{ Some(s.split(" ")) } } } object Demo11 { def main(args: Array[String]): Unit = { val str = "hello world haah kkkk"
str match { case MyArray(a,b,rest$_*)=> println(s"匹配序列 $a,$b") case _=> } } }
|
隐式匹配
1 2 3 4 5 6
| object Demo13 { def main(args: Array[String]): Unit = { val (a,b)=fun } def fun = (1,2) }
|
样例类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
case class People(name:String,var age:Int) object Demo12 {
def main(args: Array[String]): Unit = { val str =People("qq",1)
str match { case People(a, b) => println(s"样例类匹配 $a,$b") case _ => } } }
|

20模式匹配
https://jiajun.xyz/2020/11/18/scala/20模式匹配/