大数据系列修炼-Scala课程08

时间:2021-11-02 08:28:16

接下来会讲解关于各种模式匹配,从中就会知道模式匹配的重要性

关于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
//继承抽象类 --object
case 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 Item
case 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