您只能使用setter
和getter
的计算变量来覆盖超类的存储属性。我通过尝试以下代码得出了这个结论。我不确定我100%是否正确。如果我错了,请纠正我。谢谢!
class SuperClass {
var ID = 2202
}
//Wrong
class SubClass: SuperClass {
override var ID = 2203
}
//worng
class SubClass: SuperClass {
override var ID: Int {
return super.ID + 1
}
}
//Correct
class SubClass: SuperClass {
override var ID: Int {
get {
return super.ID + 1
}
set {
}
}
}
答案 0 :(得分:1)
您不能覆盖超类的存储属性,但可以从子类更改其值:
class SuperClass {
var ID = 2202
}
class SubClass: SuperClass {
override init() {
super.init()
self.ID = 2203
}
}
let a = SuperClass()
let b = SubClass()
print(a.ID)
print(b.ID)