gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 08:32:21 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com