20模式匹配

本文最后更新于 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 {
//aa相当于新声明的变量,即使前面有aa也是不去找前面的,如果要当作常量 要使用 `a`
//如果是大写的 比如Aa,scala就会去找一个 已经定义好的常量Aa
//如果想匹配到又不用 就用 _
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("运算符:")


//任意的语法结构都有值
//match相当于最后一行的值
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)

//泛型数组匹配,底层实际上不是泛型
//其余所有泛型,模式匹配无法匹配出来具体的泛型 比如List等等
//有泛型擦除,编译后泛型就不存在了
//所以泛型一般用 List[_]来匹配
ints match {
// case b:Array[Int] => println("Array[Int]")
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)

//泛型数组匹配,底层实际上不是泛型
//其余所有泛型,模式匹配无法匹配出来具体的泛型 比如List等等
//有泛型擦除,编译后泛型就不存在了
ints match {
// case b:Array[Int] => println("Array[Int]")
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 b:Array[Int] => println("Array[Int]")
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 b:Array[Int] => println("Array[Int]")
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{

//unapply方法
//返回值是Option 解决空指针
//Option有两个子类 Some None
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")

//匹配Option
strings match {

case People(age,name)=> println(s"解析people即调用unapply $age,$name")
case _=>
//为null走这里
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(" "))
//也可以 这是 后面rest匹配到的就是List
//Some(s.split(" ").toList)
}
}
}
object Demo11 {
def main(args: Array[String]): Unit = {
val str = "hello world haah kkkk"

//匹配序列
str match {
//调用unapplySeq 返回也是Option
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
//样例类
//实现了 apply unapply equals hashCode ......
//val可以不加
case class People(name:String,var age:Int)
object Demo12 {

def main(args: Array[String]): Unit = {
val str =People("qq",1)


str match {
//调用unapplySeq 返回也是Option
case People(a, b) => println(s"样例类匹配 $a,$b")
case _ =>
}
}
}

image-20201118000906519


20模式匹配
https://jiajun.xyz/2020/11/18/scala/20模式匹配/
作者
Lambda
发布于
2020年11月18日
更新于
2021年8月5日
许可协议