看起来可以通过以下方式更改类上具有trait的方法的实现:
trait Abstract { self: Result =>
override def userRepr = "abstract"
}
abstract class Result {
def userRepr: String = "wtv"
}
case class ValDefResult(name: String) extends Result {
override def userRepr = name
}
val a = new ValDefResult("asd") with Abstract
a.userRepr
这里提供现场代码:http://www.scalakata.com/52534e2fe4b0b1a1c4daa436
但是现在我想要调用函数的上一个或超级实现,如下所示:
trait Abstract { self: Result =>
override def userRepr = "abstract" + self.userRepr
}
或者trait Abstract { self: Result =>
override def userRepr = "abstract" + super.userRepr
}
然而,这些替代方案均无法编译。有任何想法如何实现吗?