Scala Trait 的线性化

本文通过具体代码示例解释了Scala中Trait如何实现super方法的线性化调用过程,展示了不同组合下Trait继承的调用顺序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Scala Trait调用super方法的线性化是一个难理解的内容,下面让我通过一段代码来推演线性化过程。

源代码:

package c5soft

class SuperBase {
  def show() {
    println("Here step to class SuperBase")
    println("I'm " + toString + "\n")
  }
}

trait TraitBase extends SuperBase {
  override def toString = "trait TraitBase"
  override def show() {
    println("Here step to trait TraitBase")
    super.show()
  }
}

trait A extends TraitBase {
  override def toString = "trait A"
  override def show() {
    println("Here step to trait A")
    super.show()
  }
}

trait AChild extends A {
  override def toString = "trait AChild"
  override def show() {
    println("Here step to trait AChild")
    super.show()
  }
}


trait B extends TraitBase {
  override def toString = "trait B"
  override def show() {
    println("Here step to trait B")
    super.show()
  }
}

trait BChild extends B {
  override def toString = "trait BChild"
  override def show() {
    println("Here step to trait BChild")
    super.show()
  }
}


class C extends SuperBase {
  override def toString = "class C"
  override def show() {
    println("Here step to class C")
    super.show()
  }
}

object Main extends App {
  println("----------C.show-----")
  new C show()

  println("----------CA.show-----")
  new C with A show()

  println("----------CB.show-----")
  new C with B show()

  println("---------CAB.show----")
  new C with A with B show()

  println("---------CaB.show----")
  new C with AChild with B show()

  println("---------CBA.show----")
  new C with B with A show()

  println("---------CBa.show----")
  new C with B with AChild show()

  println("---------Cab.show----")
  new C with AChild with BChild show()

  println("---------Ca.show----")
  new C with AChild show()

}

 

输出结果:

----------C.show-----
Here step to class C
Here step to class SuperBase
I'm class C

----------CA.show-----
Here step to trait A
Here step to trait TraitBase
Here step to class C
Here step to class SuperBase
I'm trait A

----------CB.show-----
Here step to trait B
Here step to trait TraitBase
Here step to class C
Here step to class SuperBase
I'm trait B

---------CAB.show----
Here step to trait B
Here step to trait A
Here step to trait TraitBase
Here step to class C
Here step to class SuperBase
I'm trait B

---------CaB.show----
Here step to trait B
Here step to trait AChild
Here step to trait A
Here step to trait TraitBase
Here step to class C
Here step to class SuperBase
I'm trait B

---------CBA.show----
Here step to trait A
Here step to trait B
Here step to trait TraitBase
Here step to class C
Here step to class SuperBase
I'm trait A

---------CBa.show----
Here step to trait AChild
Here step to trait A
Here step to trait B
Here step to trait TraitBase
Here step to class C
Here step to class SuperBase
I'm trait AChild

---------Cab.show----
Here step to trait BChild
Here step to trait B
Here step to trait AChild
Here step to trait A
Here step to trait TraitBase
Here step to class C
Here step to class SuperBase
I'm trait BChild

---------Ca.show----
Here step to trait AChild
Here step to trait A
Here step to trait TraitBase
Here step to class C
Here step to class SuperBase
I'm trait AChild

仔细分析这段代码与输出结果,你就能明了Trait的super调用是如何线性化的。

已CBa.show为例:

  println("---------CBa.show----")       //小写的a代表AChild
  new C with B with AChild show()  //这里等同于(new C with B with AChild).show()

调用过程从右到左并依次调用其基类的过程为:

Here step to trait AChild
Here step to trait A
Here step to trait TraitBase
Here step to class SuperBase
Here step to trait B
Here step to trait TraitBase
Here step to class SuperBase
Here step to class C
Here step to class SuperBase

按照【1.基类调用只出现一次;2.后面出现的基类调用在前面删除】原则处理如下:

Here step to trait AChild
Here step to trait A
Here step to trait TraitBase
Here step to class SuperBase
Here step to trait B
Here step to trait TraitBase
Here step to class SuperBase
Here step to class C
Here step to class SuperBase

最终得到:
Here step to trait AChild
Here step to trait A
Here step to trait B
Here step to trait TraitBase
Here step to class C
Here step to class SuperBase

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值