Scala, class using mutable var, update in overriding method

雨燕双飞 提交于 2019-12-02 12:35:19

You can use an early initializer:

case MyClass(...) extends {
  var mutableArray: ArrayBuffer[Int] = ArrayBuffer.fill(5)(0)
} with SomeClass(..) {

Probably you are hitting the infamous "one question FAQ" about initialization order.

If the method is invoked by the superclass constructor, then your initialization happens after that, resetting the data to zero.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!