scala如何处理类扩展了一个具有泛型的类?

时间:2014-11-04 03:10:30

标签: scala compilation

class Person extends Friend[Person] 

如何在扩展朋友[人]课程时对他进行解释?

1 个答案:

答案 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
  }
}