8

给定两个独立的特征:

trait T1 {
    def x = 42
}

trait T2 {
    def x = 0
}

如果我尝试定义一个混合这两个特征的类,例如:

class C extends T1 with T2

我得到一个编译器错误:

error: overriding method x in trait T1 of type => Int;
method x in trait T2 of type => Int needs `override' modifier
class C extends T1 with T2
      ^
one error found

现在假设 T1 和 T2 是独立开发的,因此没有覆盖,因为它们没有覆盖任何东西。那么如何定义 C 呢?像这样:

class C extends T1 with T2 {
    override def x = super.x
}

?

4

1 回答 1

10

这被称为钻石问题。在 Scala 中有两种方法可以解决这个问题:

trait T1 {
  def x = 0
}

trait T2 {
  def x = 42
}

class C extends T1 with T2 {
  override def x = super.x
}

class D extends T2 with T1 {
  override def x = super.x
}

如果你new C().x现在调用,你会得到42,因为 Scala 使用了你最后混合的特征的实现。new D().x顺便0说一句。这意味着,为了解决菱形问题,您必须明确定义要使用的实现。

另一种方式如下:

trait T {
  def x: Int
}

trait T1 extends T {
  override def x = 0
}

trait T2 extends T {
  override def x = 42
}

class C extends T1 with T2

A call to new C().x will still yield 42 because T2 is the last trait mixed in. The difference is that you don't have to define x in C.

于 2010-05-05T11:26:34.990 回答