目录
7.java switch-case 与 scala match-case 的关联关系:
0.scala模式匹配
结构:变量 match { case 变量值 => 功能语句 }
特殊点:(1)值为下划线,则代表了不满足以上所有情况下的默认情况
(2)只要一个case分支满足并处理了,就不会继续判断下一个case分支(不需要break)
源头:后面的各种类型的匹配,本质上都是对传入变量的处理,只是扩展而已
1.对变量的值进行模式匹配
事例1:普通匹配
def judgeGrade(grade: String) {
grade match {
case "A" => println("Excellent")
case "B" => println("Good")
case "C" => println("Just so so")
case _ => println("you need work harder")
}
}
事例2:case 表达式中(if保护语句)
def judgeGrade(name: String, grade: String) {
grade match {
case "A" => println(name + ", you are excellent")
case "B" => println(name + ", you are good")
case "C" => println(name + ", you are just so so")
case _ if name == "leo" => println(name + ", you are a good boy, come on")
case _ => println("you need to work harder")
}
}
事例3:将expression值放在 “功能语句” 中
def judgeGrade(name: String, grade: String) {
grade match {
case "A" => println(name + ", you are excellent")
case "B" => println(name + ", you are good")
case "C" => println(name + ", you are just so so")
case _grade if name == "leo" => println(name + ", you are a good boy, come on, your grade is " + _grade)
case _grade => println("you need to work harder, your grade is " + _grade)
}
}
2.对类型进行模式匹配
结构:
“case 变量: 类型 => 功能语句"
并不匹配:
“case 类型 => 功能语句"
事例:ERROR
def processException(e: Exception) {
e match {
case IllegalArgumentException => println("you have illegal arguments! exception is: ")
case FileNotFoundException => println("cannot find the file you need read or write!, exception is: ")
case IOException => println("you got an error while you were doing IO operation! exception is: " )
case Exception => println("cannot know which exception you have!" )
}
}
事例:SUCCESS
def processException(e: Exception) {
e match {
case e1: IllegalArgumentException => println("you have illegal arguments! exception is: " + e1)
case e2: FileNotFoundException => println("cannot find the file you need read or write!, exception is: " + e2)
case e3: IOException => println("you got an error while you were doing IO operation! exception is: " + e3)
case _: Exception => println("cannot know which exception you have!" )
}
}
processException(new IOException("get data from socket is fail!"))
理解:
scala 的 match-case 本质上还是先获取变量,在去判断它的类型等其他元数据
3.匹配 Array
结构:
Array mathch{case Array => 功能语句}
适用:(1)指定元素的数组
(2)指定个数元素的数组
(3)以某元素打头的数组
事例:
def greeting(arr: Array[String]) {
arr match {
case Array("Leo") => println("Hi, Leo!")
case Array(girl1, girl2, girl3) => println("Hi, girls, nice to meet you. " + girl1 + " and " + girl2 + " and " + girl3)
case Array("Leo", _*) => println("Hi, Leo, please introduce your friends to me.")
}
}
4.匹配List
结构:
List match {case List => 功能语句}
事例:
def greeting(list: List[String]) {
list match {
case List("Tom",_*) => println("Hi, Leo!")
case girl1 :: girl2 :: girl3 :: Nil => println("Hi, girls, nice to meet you. " + girl1 + " and " + girl2 + " and " + girl3)
case "Leo" :: tail => println("Hi, Leo, please introduce your friends to me.")
case _ => println("hey, who are you?")
}
}
array 与 list 语法比较:
case 后面的条件不同
源于 Array 与 List 的数据结构不同
它们的本质都是传递里面的变量给 match-case 进行匹配
遍历集合的优势:省去了循环过滤的步骤
5.匹配(case class)
(1)结构:Class(case类) match{case Class(case类) => 功能语句}
例子:
class Person
case class Teacher(name: String, subject: String) extends Person
case class Student(name: String, classroom: String) extends Person
def judgeIdentify(p: Person) {
p match {
case Teacher(name, subject) => println("Teacher, name is " + name + ", subject is " + subject)
case Student(name, classroom) => println("Student, name is " + name + ", classroom is " + classroom)
case _ => println("Illegal access, please go out of the school!")
}
}
(2)与之前 第2点 “匹配对象的类” 比较,这里的结构为什么有变化?含义是什么?
特点:
在创建 Class 时,前面加上 case 时,
这里相当于把 case Class 这种当成了一个变量去进行声明,在这里处理时,类似变量的处理方式,所以结构相似
(变量 match { case 变量值 => 功能语句 } )
6.Option与模式匹配
scala的特殊类型: Option
特点:
Option有两种值,一种是Some,表示有值,一种是None,表示没有值
结构:
变量 match {case Some(变量) => 功能语句}
例子:
val grades = Map("Leo" -> "A", "Jack" -> "B", "Jen" -> "C")
def getGrade(name: String) {
val grade = grades.get(name)
grade match {
case Some(grade) => println("your grade is " + grade)
case None => println("Sorry, your grade information is not in the system")
}
}
7.java switch-case 与 scala match-case 的关联关系
java 中的结构:
switch(expression){
case value :
//语句
break; //可选
case value :
//语句
break; //可选
//你可以有任意数量的case语句
default : //可选
//语句
}
特点比较:
(1)scala 不需要 break
(2)default =》 “_”
(3)对比 java为什么使用 lamda 表达式
使用 “ => ” 替代了 “ :”
(4)不能直接使用expression
(5)只针对变量的值进行匹配(只针对基本数据类型)
8.传递单个变量与传递集合的方式比较
区别:在功能语句中使用 expression 时,前者需要 下划线“_”, 后者不用
例子:
def greeting(list: List[String]) {
list match {
case List("Tom",_*) => println("Hi, Leo!")
case girl1 :: girl2 :: girl3 :: Nil => println("Hi, girls, nice to meet you. " + girl1 + " and " + girl2 + " and " + girl3)
case "Leo" :: tail => println("Hi, Leo, please introduce your friends to me.")
case _ => println("hey, who are you?")
}
}
与
def judgeGrade(name: String, grade: String) {
grade match {
case "A" => println(name + ", you are excellent")
case "B" => println(name + ", you are good")
case "C" => println(name + ", you are just so so")
case _grade if name == "leo" => println(name + ", you are a good boy, come on, your grade is " + _grade)
case _grade => println("you need to work harder, your grade is " + _grade)
}
}