开发者

In Scala, how do I initialise abstract vals in traits?

Suppose I have the following trait with two abstract vals

trait Base {
  val startDate: java.util.Date
  val endDate: java.util.Date
}

I now have an abstract class extending the trait

abstract class MyAbstract extends Base ...

I now wish to instantiate the abstract class with a few other traits mixed in.

  def main(args: Array[String]) {
   new MyAbstract with MixIn1 with MixIn2
  }

How to I pass in concrete values for开发者_高级运维 startDate and endDate?


Because MyAbstract is an abstract class, you can't instantiate it directly. You need to either subclass it explicitly, or create an instance of an anonymous subclass, e.g.

def main(args: Array[String]) {
  val myInstance = new MyAbstract with MixIn1 with MixIn2 {
    val startDate = ...
    val endDate = ...
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜