case class:
1.定义为case class 的类在实例化时,可以不使用new 关键字。
case class People(name:String, age:Int) val zhangsan = People("zhangsan", 30)
2. 定义为case class 的类 默认实现了 equals 和hashcode 方法
3. 默认是可序列化的
4. 其构造函数的参数 是public 访问的。如 zhangsan.name
5.支持模式匹配:
scala> case class PhoneExt(name: String, ext: Int)
defined class PhoneExt scala> val extensions = List(PhoneExt("steve", ), PhoneExt("robey", ))
extensions: List[PhoneExt] = List(PhoneExt(steve,), PhoneExt(robey,)) scala> extensions.filter { case PhoneExt(name, extension) => extension < }
res0: List[PhoneExt] = List(PhoneExt(steve,))