如何访问“覆盖” Scala 中的内部类?
我有两个特征,一个扩展另一个,每个都有一个内部类,一个扩展另一个,具有相同的名称:
trait A {
class X {
def x() = doSomething()
}
}
trait B extends A {
class X extends super.X {
override def x() = doSomethingElse()
}
}
class C extends B {
val x = new X() // here B.X is instantiated
val y = new A.X() // does not compile
val z = new A.this.X() // does not compile
}
How do I access AX
class in the C
class's身体?重命名 BX
而不是隐藏 AX
并不是首选方法。
让事情变得有点复杂的是,在我遇到这个问题的情况下,特征具有类型参数(本示例中未显示)。
I have two traits, one extending the other, each with an inner class, one extending the other, with the same names:
trait A {
class X {
def x() = doSomething()
}
}
trait B extends A {
class X extends super.X {
override def x() = doSomethingElse()
}
}
class C extends B {
val x = new X() // here B.X is instantiated
val y = new A.X() // does not compile
val z = new A.this.X() // does not compile
}
How do I access A.X
class in the C
class's body? Renaming B.X
not to hide A.X
is not a preferred way.
To make things a bit complicated, in the situation I have encountered this problem the traits have type parameters (not shown in this example).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于那些对这个奇异问题感兴趣的人,我发现它也可以用作函数的返回值。由于我的特征 A 和 B 有类型参数,因此应该会导致更简洁的代码:
For those interested in this exotic issue, I have discovered it works also as a return value of a function. Since my traits A and B have type parameters, it should lead to more concise code: