这似乎是一个令人困惑的问题,但实际上非常简单。我想做这样的事情,但 Swift 不允许这样做:
class A {}
class B : A {}
class C {
var prop: A?
}
class D : C {
override var prop : B?
}
最佳答案
取自here :
Overriding Property Getters and Setters
You can provide a custom getter (and setter, if appropriate) to override any inherited property, regardless of whether the inherited property is implemented as a stored or computed property at source. The stored or computed nature of an inherited property is not known by a subclass—it only knows that the inherited property has a certain name and type. You must always state both the name and the type of the property you are overriding, to enable the compiler to check that your override matches a superclass property with the same name and type.
看来你不能那样做。
关于Swift:覆盖也是父类属性类的子类的子类属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36677294/