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

关于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

最新文章

  1. linux命令二
  2. Spring AOP /代理模式/事务管理/读写分离/多数据源管理
  3. CSS3伸缩布局Flex学习笔记
  4. python学习笔记4(文件操作)
  5. CSS3属性transform详解
  6. struts2多文件上传(带进度条)demo+说明
  7. Css3 Media Queries移动页面的样式和图片的适配问题(转)
  8. 2013 ACM区域赛长沙 I LIKE vs CANDLE(ZOJ3734) 很好的一道树形DP
  9. delphi中WEBBrowser网页html相互调用(一)
  10. Centos6.5 安装 MariaDB-10.0.20-linux-x86_64.tar.gz
  11. AcroExch.Rect 单位、属性问题
  12. Windows的免費hMailServer搭配SpamAssassin過濾垃圾郵件:安裝與設定
  13. R语言数据处理
  14. PHP批量审核后台
  15. struts 2吊牌s:if 、s:iterator注意
  16. JavaBean编程的基本思路-逻辑业务层
  17. OC与Swift创建pod
  18. 【踩坑】360安全浏览器“极速模式”和“兼容模式”,套路还是bug?
  19. 安卓手机测试常见BUG
  20. JDK自带的Timer类

热门文章

  1. android学习经常使用的数据文件夹
  2. Children’s Queue
  3. git bash 出现vim的时候怎么退出
  4. HDU 2017 一系列统计数据
  5. 1.cocos2dx 3.2环境结构
  6. iOS6之后 NSAttributedString 福利
  7. maple 教程
  8. Socket方法LAN多线程文件传输
  9. ORA-38760: This database instance failed to turn on flashback database 第三篇
  10. 有意练习--Rails RESTful(一)