Scala中的模式匹配

Scala中的模式匹配

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package com.anthony

/**
* @ Description:
* @ Date: Created in 15:07 10/07/2018
* @ Author: Anthony_Duan
*/
object match_test extends App {
/**
* 基本的模式匹配 带守卫(判断条件)的模式匹配
*
* @param name
* @param grade
*/
def judgeGrade(name: String, grade: String): Unit = {
grade match {
case "A" => println("best")
case "B" => println("good")
case "C" if name == "张三" => println(name + "just so so")
case "C" => println("just so so")
case _ => println("bad")
}
}

judgeGrade("张三", "C")
judgeGrade("李四", "C")

println("===================我是分割线========================")

/**
* 数组的模式匹配
*
* @param array
*/
def greeting(array: Array[String]): Unit = {
array match {
case Array("张三") => println("只有张三")
case Array(x, y) => println("只有两个人")
case Array("张三", _*) => println("第一个是张三,且至少是三个人")
case _ => println("开头不是张三,至少是3个人")
}
}

greeting(Array("张三"))
greeting(Array("李四", "张三"))
greeting(Array("张三", "王五", "小明"))
greeting(Array("小明", "王五", "小雪"))

println("===================我是分割线========================")

/**
* List模式匹配
*
* @param list
*/
def greeting(list: List[String]): Unit = {
list match {
case "张三" :: Nil => println("只有张三")
case x :: y :: Nil => println("只有两个人")
case "张三" :: tail => println("张三是第一个,至少2个人")
case _ => println("开头不是张三,至少是3个人")
}
}

greeting(List("张三"))
greeting(List("李四", "张三"))
greeting(List("张三", "王五", "小明"))
greeting(List("小明", "王五", "小雪"))

println("===================我是分割线========================")

/**
* 类型的模式匹配
*
* @param obj
*/
def matchType(obj: Any): Unit = {
obj match {
case x: Int => println("Int")
case x: String => println("String")
case x: Map[String, Int] => x.foreach(println) //Map[String, Int] 这里只能限定到Map类型 Map中的键值对的类型限定不了
case _ => println("other type")
}
}
matchType(1) //Int
matchType("1") //String
matchType(1f) //other type
matchType(Map("anthony" -> 20)) //(anthony,20)
matchType(Map("anthony" -> "duan")) //(anthony,duan)
matchType(Map(20 -> "duan")) //(20,duan)

println("===================我是分割线========================")

/**
* Scala的异常处理用模式匹配
*/
try {
println("打开资源")
} catch {
case e: Exception => println(e.printStackTrace())
} finally {
println("关闭资源")
}

println("===================我是分割线========================")

/**
* case class 模式匹配
*/
class People

case class CTO(name: String, floor: Int) extends People

case class Employee(name: String, floor: Int) extends People

case class Other() extends People

def caseClassmatch(people: People): Unit = {
people match {
case CTO(name, floor) => println(name + ":" + floor)
case Employee(name, floor) => println(name + ":" + floor)
case Other() => println("other")
}
}

caseClassmatch(CTO("boos", 22))
caseClassmatch(Employee("anthony", 21))
caseClassmatch(Other())

println("===================我是分割线========================")
/**
* Sonme None 模式匹配
*/
val grades = Map("anthony" -> "A", "duan" -> "B")

def geradesMatch(name: String): Unit = {
val grade = grades.get(name)
grade match {
case Some(grade) => println(name + ":" + grade)
case None => println("None")
}
}
geradesMatch("anthony")
geradesMatch("duan")
geradesMatch("zhangsan")
}
-------------End Of This ArticleThank You For Reading-------------