2
我認爲以下2代碼快照將是等效的,但他們產生不同的結果。你能解釋爲什麼嗎?斯卡拉不同的行爲,當在特性混合飛行
1)性狀加入到類聲明:
trait M1 {
override def toString() = "m1"
}
trait M2 {
override def toString() = "m2"
}
class C extends M1 with M2 {
override def toString() = {
s"C and ${super.toString()}"
}
}
val c = new C
c.toString() //C and m2
2)性狀在飛行中加入:
class C {
override def toString() = {
s"C and ${super.toString()}"
}
}
val c = new C with M1 with M2
c.toString() //m2