properties – 为什么不在init块编译中初始化变量接口属性的代码?

interface A {
    var a: Int
}

class AJunior : A {
    override var a: Int

    init {
        a = 3
    }
}

它不会编译,因为

Property must be initialized or be abstract

但它被初始化了.我知道我可以写:

override var a: Int = 3

但为什么第一个例子不会编译?我的猜测是,这是一个简化编译器实现的错误或故意限制,但我不确定.

最佳答案 I reported this as a bug,但事实证明这种行为是设计原因:

you could have code in the init block that could observe the property in its uninitialized state

点赞