下面的代码片段
class A {
def foo = "A.foo"
}
trait B {
def foo = "B.foo"
def bar = "B.bar"
}
val x = new A with B
不编译,因为
error: overriding method foo in class A of type => java.lang.String;
method foo in trait B of type => java.lang.String needs `override' modifier
但是,我的意图是定义 x 以便:
x.foo => "A.foo"
x.bar => "B.par"
也就是说,我只希望 x 从 B 继承 bar,而不是 foo。有没有办法在 scala 中实现这一目标?