【发布时间】:2014-11-04 03:10:30
【问题描述】:
class Person extends Friend[Person]
Person 在扩展 Friend[Person] 类时是如何解释的?
【问题讨论】:
-
不清楚你在问什么。请详细说明并解释您不了解的内容。
标签: scala compilation
class Person extends Friend[Person]
Person 在扩展 Friend[Person] 类时是如何解释的?
【问题讨论】:
标签: scala compilation
如果class A extends B[A],类A 显示为自身的类型构造函数参数。这次重新出现并没有什么特别之处,即与class A extends B[C]没有什么不同。
例如,如果 B 是 trait 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
}
}
【讨论】: