大資料數列修鍊-Scala課程08

來源:互聯網
上載者:User

標籤:

接下來會講解關於各種模式比對,從中就會知道模式比對的重要性

關於Type、Array、List、Tuple模式解析

  1.Type模式比對代碼解析

    //關於Type類型的模式比對    //匹配 Int類型、string類型、Map類型[_,_]代表任意類型的k,v    def match_type(t : Any) = t match {    case p : Int => println("It is Integer")    case p : String => println("It is String, the content is : " + p)    case m: Map[_, _] => m.foreach(println)      case _ => println("Unknown type!!!")    }    match_type(2)    match_type("Spark")    match_type(Map("Scala" -> "Spark"))

  2.Array模式比對代碼解析

    //關於Array模式比對    def match_array(arr : Any) = arr match {      //匹配數組資料第一個為0    case Array(0) => println("Array:" + "0")     //匹配數組兩個數字    case Array(x, y) => println("Array:" + x + " " +y)      case Array(0, _*) => println("Array:" + "0 ...")      case _ => println("something else")    }    match_array(Array(0))    match_array(Array(0,1))    match_array(Array(0,1,2,3,4,5))

  3.List模式比對代碼解析

        //匹配list類型資料 用::來匹配    def match_list(lst : Any) = lst match {    case 0 :: Nil => println("List:" + "0")    case x :: y :: Nil => println("List:" + x + " " + y)    case 0 :: tail => println("List:" + "0 ...")    case _ => println("something else")    }    match_list(List(0))    match_list(List(3,4))    match_list(List(0,1,2,3,4,5))

  4.Tuple,模式比對代碼解析

    //tuple模式比對 -->tuple模式是比較隨意的    def match_tuple(tuple : Any) = tuple match {    case (0, _) => println("Tuple:" + "0")    case (x, 0) => println("Tuple:" + x )    case _ => println("something else")    }        match_tuple((0,"Scala"))    match_tuple((2,0))    match_tuple((0,1,2,3,4,5))
關於Scala中提取器Extractor解析

  1.Extractor實戰解析

  2.Extractor源碼解析:提取器就好比apply函數一樣,擷取使用者的

    //提取器就是擷取函數中參數 從中擷取資料  -->數組的模式比對就是提取器擷取資料    def match_array(arr : Any) = arr match {    case Array(0) => println("Array:" + "0")     case Array(x, y) => println("Array:" + x + " " +y)      case Array(0, _*) => println("Array:" + "0 ...")      case _ => println("something else")    }        match_array(Array(0))    match_array(Array(0,1))    match_array(Array(0,1,2,3,4,5))        //正則式與模式比對結合形成了一個函數就擷取匹配出的數值與字元    val pattern = "([0-9]+) ([a-z]+)".r    "20150628 hadoop" match {      case pattern(num, item) => println(num + " : " + item)    }

 

Case class和Case object代碼解析

  1.Case class代碼解析:模式比對中使用case class和case object中訊息傳遞中用的比較多,能夠根據業務進行訊息的發送

  2.Case object代碼解析:可以理解為單例模式

object case_class_object {  def main(args: Array[String]): Unit = {      //定義一個模式比對      def caseOps(person: Person) =  person match {      case Student(age) => println("I am " + age + "years old")      case Worker(_, salary) => println("Wow, I got " + salary)      case Shared => println("No property")      }     caseOps(Worker(1,19))    caseOps(Student(19))    caseOps(Shared)        //因為繼承時候會發現參數都是常量-->需要改變的時候可以複製對象進行改變    val worker = Worker(29, 10000.1)    val worker2 = worker.copy(salary = 19.95)    val worker3 = worker.copy(age = 30)  }}//case class object在訊息傳輸過程中起很重要地位//抽象類別abstract class Person//繼承抽象類別 帶有Int類型的常量case class Student(age: Int) extends Person//繼承抽象類別 帶有Int類型與double的常量case class Worker(age: Int, salary: Double) extends Person//繼承抽象類別 --objectcase object Shared extends Person
模式比對進階實戰:嵌套的Case class

  1.嵌套的Case class解析:在匹配模式中參數也使用case class和caseobject

  2.Case object實戰解析

def main(args: Array[String]): Unit = {        //定義一個模式比對 case class      def caseclass_nested(person: Item) =  person match {        //匹配項中第一二個參數可以不計,第三課參數用art代替Book對象用@符號引用類似也這樣        case Bundle(_, _, art @ Book(_, _), rest @ _*) => println(art.description + " : " + art.price)        case Bundle(_, _, Book(descr, _), _*) => println("The first description is :" + descr)        case _ => println("Oops!")      }      //調用定義的模式比對      caseclass_nested(Bundle("1111 Special‘s", 30.0,Book("Scala for the Spark Developer", 69.95),      Bundle("Hadoop", 40.0,Book("Hive", 79.95),Book("HBase", 32.95))))     caseclass_nested(Bundle("1212 Special‘s", 35.0, Book("Spark for the Impatient", 39.95)))   }  }abstract class Itemcase class Book(description: String, price: Double) extends Item//繼承抽象類別,並且參數中嵌套了抽象類別 -->這就是類中嵌套類case class Bundle(description: String, price: Double, items: Item*) extends Item

就講這麼多了,明天繼續....

希望大家關注王家林老師的視頻,本系列也是從他的視頻中學習的,他是號(18610086859)

百度雲地址:http://pan.baidu.com/s/1kTw4Idp

大資料數列修鍊-Scala課程08

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.