我在斯卡拉有關性狀的繼承
class MI {
def identity : Int = {
println("came inside MI")
1
}
}
trait DMI extends MI {
override def identity : Int = {
println("came inside DMI")
2 * super.identity
}
}
trait TMI extends MI {
override def identity : Int = {
println("came inside TMI")
3 * super.identity
}
}
class Foo extends TMI with DMI {
override def identity : Int = super[DMI].identity
}
val f = new Foo
f.identity
寫這段代碼的代碼的輸出混亂是
came inside DMI
came inside TMI
came inside MI
res5: Int = 6
誰能解釋爲什麼呼叫TMI裏面去?根據我的答案應該是2
閱讀:http://www.artima.com/scalazine/文章/ stackable_trait_pattern.html – Dima