【发布时间】:2018-02-01 10:13:13
【问题描述】:
我是 scala 新手,我有以下问题
abstract class A {
def foo(): List[String]
}
trait AA extends A {
override def foo(): List[String] = {
// Something like
// If super.foo is implemented, "AA" +: super.foo
// If super.foo is not implemented, List("AA")
}
}
class B extends A with AA {
override def foo(): List[String] = {
// I think the trait will have the implementation, so I should only need to do:
super.foo
}
}
基本上我希望每个特征都在 foo 的结果中添加一个部分,这样我就可以通过混合多个这样的特征来获得最终结果。我想我可以让 A 类中的 foo 方法返回空列表,但我只是好奇是否有办法检查父类中的方法是否已实现。
另外,如果有反模式,请告诉我。
【问题讨论】:
-
它看起来像装饰器模式,但我不确定你的目标是什么
标签: scala overriding abstract traits