这是我所类的简化:

trait RequiredThings {
  val requiredThings: Seq[String]
}

class SimpleCalculator with RequiredThings {
  val requiredThings = List("a", "b")
}

class ComplicatedCalculator extends SimpleCalculator with RequiredThings {
  self: SimpleCalculator =>
  override val requiredThings:List[String] = List("c") ::: self.requiredThings
}

在此版本中,我使用的是自类型注释,但我并不完全肯定这是要走的路。我想我可以通过将requiredThings始终转换为方法来使其工作,但是我想将其作为字段尝试。

最终解决方案:
trait RequiredThings {
  def requiredThings: Seq[String]
}

class SimpleCalculator with RequiredThings {
  def requiredThings = List("a", "b")
}

class ComplicatedCalculator extends SimpleCalculator with RequiredThings {
  override def requiredThings:List[String] = List("c") ::: super.requiredThings
}

最佳答案

是的,尚未实现对通过自类型“继承”的方法的 super 调用。这将很快(有所改变)。同时,您应该改用继承。

关于scala - 在Scala中,我可以覆盖包含列表的具体字段并将其附加到子类中吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2423658/

10-11 21:18