class Person extends Friend[Person]
如何在扩展朋友[人]课程时对他进行解释?
答案 0 :(得分: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
}
}