Scala: Overridden value parent code is run but value is not assigned at parent

我们两清 提交于 2019-12-01 05:53:42

问题


Running the code below:

class Parent {
  val value = {
    println("Setting value in parent")
    "ParentVal"
  }
  println(s"Parent value is ${value}")
}

class Child extends Parent {
  override val value = {
    println("Setting value in child")
    "ChildVal"
  }
  println(s"Child value is ${value}")
}

new Child

Produces this output:

Setting value in parent
Parent value is null
Setting value in child
Child value is ChildVal

So the code associated with the parent value assignment is run, however the value is not really assigned at the parent. Afterwards the child code runs and it assigns the value as expected.

Could someone explain the chain of events here at a lower level?


回答1:


You can think about val as if it were a combination of a private member variable without setters + a getter method. If you rewrite your code without vals, it would be something like this:

class Parent {
  private[this] var parentValue = {
    println("Setting value in parent")
    "ParentVal"
  }
  def value: String = parentValue
  println(s"Parent value is ${value}")
}

class Child extends Parent {
  private[this] var childValue = {
    println("Setting value in child")
    "ChildVal"
  }
  override def value = childValue
  println(s"Child value is ${value}")
}

new Child

Just as the original, it prints:

Setting value in parent
Parent value is null
Setting value in child
Child value is ChildVal

This is because the parent constructor invokes the method value, but this method is overridden by def value = childValue. The returned childValue is null, because when the parent constructor is invoked, the child constructor has not been invoked yet.

You can read more about object initialization order here.


Related answers:

  1. Why does implement abstract method using val and call from superclass in val expression return NullPointerException.

  2. Assertion with require in abstract superclass creates NPE



来源:https://stackoverflow.com/questions/50703986/scala-overridden-value-parent-code-is-run-but-value-is-not-assigned-at-parent

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