问题描述
在 Martin Odersky 的 最近关于 Scala 中程序员能力水平的帖子中,在 专家库设计者部分,他包括术语早期初始化器".
In Martin Odersky's recent post about levels of programmer ability in Scala, in the Expert library designer section, he includes the term "early initializers".
Scala 编程.它们是什么?
These are not mentioned in Programming in Scala. What are they?
推荐答案
早期初始化器是子类的构造函数的一部分,旨在在其超类之前运行.例如:
Early initializers are part of the constructor of a subclass that is intended to run before its superclass. For example:
abstract class X {
val name: String
val size = name.size
}
class Y extends {
val name = "class Y"
} with X
如果代码被写成
class Z extends X {
val name = "class Z"
}
然后当Z
被初始化时会发生空指针异常,因为在正常的初始化顺序中size
在name
之前被初始化(超类课前).
then a null pointer exception would occur when Z
got initialized, because size
is initialized before name
in the normal ordering of initialization (superclass before class).
这篇关于在 Scala 中,什么是“早期初始化器"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!