【发布时间】:2012-12-13 17:52:58
【问题描述】:
所以这是有问题的代码:
trait World {
type State
def dynamics(s: State): State
// ...
}
trait GridWorld extends World {
class State {...} // concrete
def dynamics(s: State) = s // concrete
// some other staff still abstract
}
trait SimpleGridWorld extends GridWorld {
class State extends super.State {...} // concrete
def foo {dynamics(new State)} // compiler error
}
编译器说,dynamics 在World 和GridWorld 中都匹配签名。但是,在World 中它是抽象的,然后在GridWorld 中实现,所以在我看来,很明显我在调用GridWorld.this.dynamics。
我注意到的另一件事是,如果我删除SimpleGridWorld 中的extends super.State,一切正常(我不明白为什么,我确实需要GridWorld.State 中定义的功能)。有什么解释吗?谢谢!
更新
无论如何,我看到我的设计模式很奇怪,因为如果SimpleGridWorld 中的State 没有继承GridWorld.this.State,dynamics 将引用根特征World 中定义的未实现模式(这是有道理的,因为实现在GridWorld 中可能使用GridWorld.this.State 中可能不存在的SimpleGridWorld.this.State 的功能)。但我想要的是:
-
XXXWorld.this.State必须继承它的super.State(或者直接使用它) -
dynamics总是引用super.dynamics如果在超级特征/类中实现,除非在这里被覆盖。
我该怎么做?我认为这不是一个完全无关紧要的问题,可能上一个问题的答案会告诉我如何重新设计我的模式。
【问题讨论】:
标签: scala inheritance overloading inner-classes ambiguous-call