【问题标题】:How does scala handle a class extends a class with a generic of itself?scala如何处理一个类扩展一个具有自身泛型的类?
【发布时间】:2014-11-04 03:10:30
【问题描述】:
class Person extends Friend[Person] 

Person 在扩展 Friend[Person] 类时是如何解释的?

【问题讨论】:

  • 不清楚你在问什么。请详细说明并解释您不了解的内容。

标签: scala compilation


【解决方案1】:

如果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
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多