Scala, class using mutable var, update in overriding method

后端 未结 2 403
抹茶落季
抹茶落季 2021-01-28 07:38

I am having a hard time understanding how to get the following code structure to work.

In Scala I have a class MyClass which inherits from SomeClass<

相关标签:
2条回答
  • 2021-01-28 07:48

    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.

    0 讨论(0)
  • 2021-01-28 08:01

    You can use an early initializer:

    case MyClass(...) extends {
      var mutableArray: ArrayBuffer[Int] = ArrayBuffer.fill(5)(0)
    } with SomeClass(..) {
    
    0 讨论(0)
提交回复
热议问题