在 Scala 中,什么是“早期初始化器"? [英] In Scala, what is an "early initializer"?

查看:33
本文介绍了在 Scala 中,什么是“早期初始化器"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 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被初始化时会发生空指针异常,因为在正常的初始化顺序中sizename之前被初始化(超类课前).

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 中,什么是“早期初始化器"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆