scala如何处理一个类扩展一个类的泛型本身? [英] How does scala handle a class extends a class with a generic of itself?

查看:95
本文介绍了scala如何处理一个类扩展一个类的泛型本身?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  class Person extend Friend [Person] 

如果类A扩展了B [B] A] ,类 A 作为类型构造函数参数显示。这个重现没有什么特别之处,也就是说,它与类A扩展B [C] 没有区别。



例如,如果 B 是trait 已订购

  class Person(val name:String,val age:Int)extends Ordered [Person] {
// in method`compare(that:A)`of Ordered, `替换为`Person`
def compare(that:Person):Int = {
val i = this.name compare..name
if(i!= 0)i else this .age compare the.age
}
}


class Person extends Friend[Person] 

How does Person been explained while it extends a Friend[Person] class ?

解决方案

If class A extends B[A], class A appears as a type constructor parameter of itself. There is nothing special about this reappearance, i.e. it's no different from a class A extends B[C].

For example, if B was 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
  }
}

这篇关于scala如何处理一个类扩展一个类的泛型本身?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆