案例類別相等

由 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