5

如何在 Scala 中预初始化一个 val 并扩展一个类?

例如:

object Start {
  def main(args: Array[String]): Unit = {
        new T()
        new T2()
        //T3?
  }
}

class T extends{val z = 10} with X  with Y
class T2 extends X with Y {val z=10}
//class T3 extends{val z = 10}  ??? Z with X with Y //????

class Z

trait  X {
  val z :Int
}

trait Y { this :X =>
  println("Test: "+z)  
}
4

1 回答 1

6

利用:

class T3 extends {val z = 10} with Z with X with Y

Scala 语言规范 2.9、5.1.6:

EarlyDefs ::= ‘{’ [EarlyDef {semi EarlyDef}] ‘}’ ‘with’

所以一个早期的定义总是必须遵循一个with

于 2013-05-02T21:04:30.797 回答