Scala入门系列(十一):模式匹配

小说:德州学生兼职网作者:侯北密杜更新时间:2018-11-14字数:78957

Scala入门系列(十一):模式匹配


引言

模式匹配是Scala中非常有特色,非常强大的一种功能。

类似于Java中的switch case语法,但是模式匹配的功能要比它强大得多,switch只能对值进行匹配,但是Scala的模式匹配除了可以对值进行匹配之外,还可以对类型进行匹配、对Array和List的元素情况进行匹配、对case class进行匹配甚至对有值或没值(Option)进行匹配

而且在Spark源码中也大量地使用了模式匹配功能。因此为了更好地编写Scala程序,并且更加通畅地看懂Spark源码,学好模式匹配是非常重要的。

基础语法

match case的语法如下:变量 match { case 值 => 代码}。如果值为下划线,则代表不满足以上所有情况下的默认情况如何处理。此外,match case中,只要一个case分支满足并处理了,就不会继续判断下一个case分支了。(这一点与java不同,java的switch case需要用break阻止)

对变量值进行匹配

// 案例:成绩评价
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")
}
}
judgeGrade: (grade: String)Unit
scala> judgeGrade("E")
you need work harder
scala> judgeGrade("B")
Good

在模式匹配中使用if守卫

// 案例:成绩评价(升级版)
def judgeGrade(name: String, grade: String){
grade match {
case "A" => println("Excellent")
case "B" => println("Good")
case "C" => println("Just so so")
case _ if name == "leo" => println(name + ", you are a good boy, come on")
case _ => println("you need to work harder")
}
}
judgeGrade: (name: String, grade: String)Unit
scala> judgeGrade("leo", "E")
leo, you are a good boy, come on
scala> judgeGrade("sparks", "E")
you need to work harder

在模式匹配中进行变量赋值

模式匹配中可以将默认情况即下划线替换为一个变量名,这样模式匹配语法就会将要匹配的值赋值给这个变量,从而可以在后面的处理语句中使用要匹配的值。

// 案例:成绩评价(升级版)
def judgeGrade(grade: String){
grade match {
case "A" => println("you got A grade, excellent!")
case "B" => println("you got B grade, good")
case "C" => println("you got C grade, so so")
case badGrade => println("you got " + badGrade + "grade, i hope that you can get C next time")
}
}
judgeGrade: (grade: String)Unit
scala> judgeGrade("E")
you got Egrade, i hope that you can get C next time
scala> judgeGrade("F")
you got Fgrade, i hope that you can get C next time

类型匹配

Scala的模式匹配强大之处就在于可以直接匹配类型而不仅仅是值,这也是Java中switch case绝对做不到的。

匹配类型的语法为case 变量 : 类型 => 代码

// 案例:异常处理
import java.io._
def processException(e: Exception){
e match {
case e1:IllegalArgumentException => println("you passed illegal argument, exception is : " +e1)
case e2:IOException => println("you got an error while doing IO operation!" + e2)
case _:Exception => println("cannot know which exception you have")
}
}
processException: (e: Exception)Unit
// 测试能否匹配类型
scala> processException(new IllegalArgumentException("expect two arguments, but found only one"))
you passed illegal argument, exception is : java.lang.IllegalArgumentException: expect two arguments
, but found only one
scala> processException(new ArrayIndexOutOfBoundsException("array is null."))
cannot know which exception you have

对Array和List的元素进行匹配

对Array进行模式匹配,分别可以匹配带有指定元素的数组、带有指定个数的数组、以某元素打头的数组。

def greeting(arr: Array[String]) {
arr match {
case Array("Leo") => println("Hi, Leo!")
case Array(girl1,girl2,girl3) => println("Hi, girls, may I know you name? "+girl1+" "+girl2+" "+
girl3)
case Array("Leo", _*) => println("Hi, leo, please introduce your friends to me.")
case _ => println("hey, who are you?")
}
}
greeting: (arr: Array[String])Unit
// 测试
scala> greeting(Array("Leo"))
Hi, Leo!
scala> greeting(Array("jen", "Alice", "lory"))
Hi, girls, may I know you name? jen Alice lory
scala> greeting(Array("sparks"))
hey, who are you?

  
对List进行模式匹配,与Array类似,但是需要使用List特有的 :: 操作符。

def greeting(list: List[String]){
list match {
case "Leo" :: Nil => println("Hi, Leo!")
case girl1 :: girl2 :: girl3 ::Nil => println("Hi, girls, nice to meet you." + girl1 + " " + gir
l2 + " " + girl3)
case "Leo" :: tail => println("Hi, Leo, please introduce your friends to me.")
case _ => println("hey, who are you?")
}
}
greeting: (list: List[String])Unit
// 测试
scala> greeting(List("Leo"))
Hi, Leo!
scala> greeting(List("Marry", "Alice", "lory"))
Hi, girls, nice to meet you.Marry Alice lory
scala> greeting(List("Sparks"))
hey, who are you?

case class匹配

Scala中提供了一种特殊的类,用case class进行声明,中文也可以称作样例类。case class其实有点类似于Java中的JavaBean的概念。即只定义field,并且由Scala编译时自动提供getter和setter方法,但是没有method

case class的主构造函数接收的参数通常不需要使用val或var修饰,Scala自动就会使用val修饰(但是如果你自己使用var修饰,那么还是会按照var来定义)

Scala自动为case class定义了伴生对象,也就是object,并且定义了apply()方法,该方法接收主构造函数中相同的参数,并返回case class对象。

// 定义case class
class Person
case class Teacher(name: String, subject: String) extends Person
case class Student(name: String, classroom: String) extends Person
case class Worker(name: 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 " + classro
om)
case _ => println("Illegal access, please go out of the school")
}
}
defined class Person
defined class Teacher
defined class Student
defined class Worker
judgeIdentify: (p: Person)Unit
// 测试匹配效果
scala> val leo: Person = Student("leo", "class1")
leo: Person = Student(leo,class1)
scala> val tom: Person = Teacher("tom", "Math")
tom: Person = Teacher(tom,Math)
scala> val jack: Person = Worker("jack")
jack: Person = Worker(jack)
scala> judgeIdentify(leo)
Student, name isleo, classroom is class1
scala> judgeIdentify(tom)
Teacher, name is tom,subject is Math
scala> judgeIdentify(jack)
Illegal access, please go out of the school

Option匹配

Scala中有一种特殊的类型,叫做Option。Option有两种值,一种是Some,表示有值,一种是None,表示没有值。

Option通常会用于模式匹配中,用于判断某个变量是有值还是没有值,这比null来的更加简洁明了。

Spark源码中大量地使用了Option,比如Some(a)、None这种语法。

// 案例:成绩查询
val grades = Map("Leo" -> "A", "Jack" -> "B", "Sparks" -> "C")
def getGrade(name: String) {

我要说两句: (0人参与)

发布