我试图将我对蛋糕模式的理解转换为简单的 scala 代码,发现它没有编译。有人可以看看下面的代码并告诉我我理解模式的方式有什么问题吗?我读了这篇文章并尝试了类似的东西(http://www.cakesolutions.net/teamblogs/2011/12/19/cake-pattern-in-depth)
在下面的代码中println("This is " + userServiceComponent.whatCalc1) //> This is ()
- 我期待它打印This is ScifiCalc Calc
但它的打印This is ()
代码:-
trait Calc {
def whatCalc
}
trait NormalCalc extends Calc {
def whatCalc = new String("Normal Calc")
}
trait ScifiCalc extends Calc {
def whatCalc = new String("ScifiCalc Calc")
}
trait TestTrait{
def whatCalc1
}
trait TestCalc extends TestTrait {
this: Calc =>;
def whatCalc1 = {
whatCalc
}
}
object SelfReferenceExample {
println("Welcome to the Scala worksheet")
val userServiceComponent = new TestCalc with ScifiCalc {}
println("This is " + userServiceComponent.whatCalc1) //> This is ()
}