30.模式匹配高级实战:嵌套的Case class
来源:互联网 发布:完美循证医学数据 编辑:程序博客网 时间:2024/06/05 06:31
有时候要表达一种类似于集合类型的元素,而元素本身的表示又是case class的实例,这时候就要用到嵌套
eg:书店中有很多书,很多书可以构成集合,而书本身用case class表达,集合也用case class表达。这时候就用嵌套。
package ce.scala.pp//7abstract class Itemcase class Book(description : String, price : Double) extends Itemcase class Bundle(description : String, price : Double, item : Item*) extends Item //Item*的意思是:可以有若干个Item类型的成员,可以是Book,也可以是Bundle,类似json的表达object Pattern_Match_Case_Class_Nested_30 { def main(args: Array[String]): Unit = { def caseclass_nested(person : Item) = person match{ case Bundle(_,_, Book(descr, _), _*) => println("The first description is "+ descr ) //case Bundle(_,_,art @Book(_,_), rest @ _*) => println(art.description + " " + art.price) //art指向了Book这个对象,rest指向了_* case _ => println("oops!") } caseclass_nested(Bundle("111 special" , 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" , 35.0, Book("spark for the Impatient", 39.95))) }}
注释掉第二行,输出:
The first description is scala for the spark developer
The first description is spark for the Impatient
注释掉第一行,输出:
scala for the spark developer 69.95
spark for the Impatient 39.95
阅读全文
0 0
- 30.模式匹配高级实战:嵌套的Case class
- scala学习:模式匹配高级实战:嵌套的Case class
- scala-30:模式匹配高级实战:嵌套的Case class
- Scala深入浅出实战经典:30,模式匹配高级实战:嵌套的Case class
- scala的模式匹配和case class
- scala Case Class和模式匹配(1):简单的示例
- Scala学习笔记20【Scala 模式匹配之case class实战】
- Scala模式匹配,case 字符串,case 匹配类型,case 匹配数组,case 匹配List,case 匹配元组,case匹配case class和case object
- Scala语法 Case Class和模式匹配
- Scala 语法 Case Class和模式匹配
- scala 模式匹配与case class
- 【scala 匹配模式】match case|case class |::|:::|=>用法
- Scala 专题教程-Case Class和模式匹配(1):简单的示例
- Scala 模式匹配的五种情形,值,case class ,optional ,数据类型,集合内数据
- 第十四节 Case Class与模式匹配(一)
- 第十五节 Case Class与模式匹配(二)
- Scala基础—模式匹配与case class示例
- shell Learning [case 的合并匹配模式]
- understand软件使用教程
- 连接池配置与调用(一)
- HQL之多表查询(一对多和多对多)
- python3执行linux命令
- matlab实现MSER(最大极值稳定区域)来进行文本定位
- 30.模式匹配高级实战:嵌套的Case class
- svn报错:Previous operation has not finished; run 'cleanup' if it was interrupted的解决办法
- monkey检查内存泄漏
- 【巨杉案例】:大数据司法查询平台
- select 下拉框分页时,不兼容chrome 解决办法
- 深度学习---网络初始化
- Java Throwable类及其子类
- 剑指Offer-47
- 依赖注入和控制反转的关系