我有三个特点,我想强制其中一个叫它C
来混合特质A
和B
。即使我知道如何强制B
混合A
,我也无法做到。它可以做这样的:如何强制一个特质来实现另一个特质
trait A {
def getThree() = 3
}
trait B {
this: A =>
def getFive() = 2 + getThree()
}
trait C {
this: A => // that's the line which
// this: B => // this line is incorrect as I there is "this: A =>" already
// def getEight() = getThree() + getFive() // I want this line to compile
}
因此我可以调用该函数getEight()
object App {
def main(args: Array[String]): Unit = {
val b = new B with A {}
println(b.getFive())
// It would be cool to make these two lines work as well
// val c = new C with B with A {}
// println(c.getEight())
}
}
'这样的:A和B =>' –