2014-11-04 31 views

回答

3

如果class A extends B[A],類A顯示爲型構造函數參數自身。這種再現沒有什麼特別之處,即它與class A extends B[C]沒有什麼不同。

例如,如果B爲特徵Ordered

class Person(val name: String, val age: Int) extends Ordered[Person] { 
    // in method `compare(that: A)` of Ordered, type `A` is replaced with `Person` 
    def compare(that: Person): Int = { 
    val i = this.name compare that.name 
    if (i != 0) i else this.age compare that.age 
    } 
} 
相關問題