假设我有以下代码:
trait Trait1 {
trait Inner {
val name = "Inner1"
}
}
trait Trait2 {
trait Inner {
val name = "Inner2"
}
}
class Foo extends Trait1 with Trait2 {
// I want Concrete1 to be a Trait1.Inner not a Trait2.Inner
class Concrete1 extends Inner
val c = new Concrete1
}
object Obj {
def main(args: Array[String]): Unit = {
val foo = new Foo
println(foo.c.name)
}
}
当我混入Trait1
andTrait2
时,引用Inner
似乎默认为Inner
我混入第二个特征的类型;所以当我调用它Obj
的main
方法时,它会打印出来Inner2
。我如何引用Trait1.Inner
in Foo
?以下所有三个都会产生编译器错误:
class Concrete1 extends Trait1.Inner
class Concrete1 extends Trait1$Inner
class Concrete1 extends Trait1#Inner