1

ケーキのパターンについての理解を単純なスカラ コードに変換しようとしていたところ、コンパイルされていないことがわかりました。以下のコードを見て、パターンを理解する方法の問題を教えてください。私はこの記事を読んで、似たようなことを試していました( 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 ()
}
4

1 に答える 1