案例类别相等
由 case 类免费提供的一个功能是自动生成的 equals
方法,该方法检查所有单个成员字段的值相等,而不是仅检查对象的引用相等性。
普通类:
class Foo(val i: Int)
val a = new Foo(3)
val b = new Foo(3)
println(a == b)// "false" because they are different objects
案例类:
case class Foo(i: Int)
val a = Foo(3)
val b = Foo(3)
println(a == b)// "true" because their members have the same value