trait Base
{
val widget = new Widget {}
trait Widget
}
trait Child1 extends Base
{
override val widget = new Widget {}
trait Widget extends super.Widget
}
trait Child2 extends Base
{
override val widget = new Widget {}
trait Widget extends super.Widget
}
class Sample extends Child1 with Child2
{
override val widget = // Here be dragons
}
有没有办法在Sample中正确组合小部件?如果Child1#Widget和Child2#Widget具有可分辨名称,它可以正常工作,但我很好奇是否可以像这样访问它们。
我失败了new Child1#Widget with Child2#Widget
:“不是构造函数的合法前缀”
或
override val widget =
{
val a = this: Child1
val b = this: Child2
new a.Widget with b.Widget {}
}
“非法继承:继承trait Widget的不同类型实例”
答案 0 :(得分:2)
你可以试试这个:
class Sample extends Child1 with Child2 {
override val widget = new Widget {}
trait Widget extends super[Child1].Widget with super[Child2].Widget
}
或者只是:
class Sample extends Child1 with Child2 {
override val widget = new super[Child1].Widget with super[Child2].Widget {}
}