1. 程式人生 > >scala學習之旅(十一):模式匹配

scala學習之旅(十一):模式匹配

引言

模式匹配是scala 中非常有特色且非常強大的一種功能。模式匹配其實就是類似於java 中的switch case 語法,即對一個值進行判斷然後做出不同的操作。
但是scala 的模式匹配比java要強大很多java 的switch 語法只能對值匹配,但是scala 除了對值進行匹配,還可以對型別進行匹配,對Array 和list 的情況進行匹配 對case class 進行匹配,甚至對有值沒有值進行匹配

1.模式匹配

// Scala是沒有Java中的switch case語法的,相對應的,Scala提供了更加強大的match case語法,即模式匹配,類替代switch
case,match case也被稱為模式匹配 // Scala的match case與Java的switch case最大的不同點在於,Java的switch case僅能匹配變數的值,比123等;而Scala的match case可以匹配各種情況,比如變數的型別、集合的元素、有值或無值 // match case的語法如下:變數 match { case => 程式碼 }。如果值為下劃線,則代表了不滿足以上所有情況下的預設情況如何處理。此外,match case中,只要一個case分支滿足並處理了,就不會繼續判斷下一個case分支了。(與Java不同,java的switch case
需要用break阻止) // match case語法最基本的應用,就是對變數的值進行模式匹配 // 案例:成績評價 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") } }

在模式匹配中使用if守衛

// Scala的模式匹配語法,有一個特點在於,可以在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") } }

在模式匹配中進行變數賦值

// Scala的模式匹配語法,有一個特點在於,可以將模式匹配的預設情況,下劃線,替換為一個變數名,此時模式匹配語法就會將要匹配的值賦值給這個變數,從而可以在後面的處理語句中使用要匹配的值
// 為什麼有這種語法??思考一下。因為只要使用用case匹配到的值,是不是我們就知道這個只啦!!在這個case的處理語句中,是不是就直接可以使用寫程式時就已知的值!
// 但是對於下劃線_這種情況,所有不滿足前面的case的值,都會進入_這種預設情況進行處理,此時如果我們在處理語句中需要拿到具體的值進行處理呢?那就需要使用這種在模式匹配中進行變數賦值的語法!!

// 案例:成績評價(升級版)
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.對型別進行模式匹配

// Scala的模式匹配一個強大之處就在於,可以直接匹配型別,而不是值!!!這點是java的switch case絕對做不到的。
// 理論知識:對型別如何進行匹配?其他語法與匹配值其實是一樣的,但是匹配型別的話,就是要用“case 變數: 型別 => 程式碼”這種語法,而不是匹配值的“case => 程式碼”這種語法。

// 案例:異常處理
import java.io._

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!" )
  }
}

3.對Array和List進行模式匹配

// 對Array進行模式匹配,分別可以匹配帶有指定元素的陣列、帶有指定個數元素的陣列、以某元素打頭的陣列
// 對List進行模式匹配,與Array類似,但是需要使用List特有的::操作符

// 案例:對朋友打招呼
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.")
    case _ => println("hey, who are you?")
  }
}

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 + " and " + girl2 + " and " + girl3)
    case "Leo" :: tail => println("Hi, Leo, please introduce your friends to me.")
    case _ => println("hey, who are you?")
  }
}

4.case class與模式匹配

// Scala中提供了一種特殊的類,用case class進行宣告,中文也可以稱作樣例類。case class其實有點類似於Java中的JavaBean的概念。即只定義field,並且由Scala編譯時自動提供getter和setter方法,但是沒有method。
// case class的主建構函式接收的引數通常不需要使用var或val修飾,Scala自動就會使用val修飾(但是如果你自己使用var修飾,那麼還是會按照var來)
//  Scala自動為case class定義了伴生物件,也就是object,並且定義了apply()方法,該方法接收主建構函式中相同的引數,並返回case class物件

// 案例:學校門禁
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!")
  }  
}

5.Option與模式匹配

// Scala有一種特殊的型別,叫做Option。Option有兩種值,一種是Some,表示有值,一種是None,表示沒有值。
// Option通常會用於模式匹配中,用於判斷某個變數是有值還是沒有值,這比null來的更加簡潔明瞭
// Option的用法必須掌握,因為Spark原始碼中大量地使用了Option,比如Some(a)、None這種語法,因此必須看得懂Option模式匹配,才能夠讀懂spark原始碼。

// 案例:成績查詢
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")
  }
}

歡迎關注,更多福利

這裡寫圖片描述