Differences when overriding inherited constructor fields?

瘦欲@ 提交于 2019-12-08 19:15:46

问题


Consider this simple Scala class:

class A(val d: Int)

Is there a difference in Scala (either in behaviour or generated bytecode) between

class B(d: Int) extends A(d)

and

class B(override val d: Int) extends A(d)

or are both equivalent? If they are different, what would be the specific usecase for each of them?

Would it be different if A was defined as class A(var d: Int)?


回答1:


For vals, there is no semantic difference. However, there may be a difference in the generated bytecode. In particular, if a method defined in the derived class refers to d, it refers to the constructor parameter d rather than to the val of the same name. This is implemented via an additional private field generated for the derived class.

For vars, there is a difference in behavior. Without an override, any methods that refer to d from within the derived class will be referring to the constructor parameter, while callers referencing d from outside the class will get the field. In this case, the two values may differ (if the value has changed since construction).

Here's a session that demonstrates the behavior with a var:

scala> class A(var d: Int)
defined class A

scala> class B(d: Int) extends A(d) { override def toString = "d: " + d }
defined class B

scala> val b = new B(1)
b: B = d: 1

scala> b.d = 2

scala> b.d
res1: Int = 2

scala> b
res2: B = d: 1

This question is related: Idiomatic Scala way to deal with base vs derived class field names?.



来源:https://stackoverflow.com/questions/6657248/differences-when-overriding-inherited-constructor-fields

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