为什么在 init 块中初始化变量接口 属性 的代码不会编译?
Why won't code that initializes a variable interface property in init block compile?
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
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