Swift:覆盖也是父类属性类的子类的子类属性

标签 swift overriding subclass

这似乎是一个令人困惑的问题,但实际上非常简单。我想做这样的事情,但 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/

相关文章:

swift - 计时器不会在 CollectionView 中正确更新 Swift

swift - 如何快速将数据转换为十六进制字符串

java - 子类中扩展子类的 super() 构造函数?

c++ - 可以消除这个基类构造函数吗?

swift - UITableViewController 数据源不读取新值

iOS:有没有办法获取 View 中每个元素的背景颜色?

iOS App Delegate params 字典不包含任何相关链接

ios - 如何从 fireBase 推送通知 swift 4 获取数据

linux - linux下如何添加posix hooks?

c++ - 虚函数返回类型