我有三個特點,我想強制其中一個叫它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 =>' –