Swift:覆盖属性 class
Swift: override property class
有了 Swift 协议、扩展和约束,我想做两件事:
- 创建基础摘要class
Subclass 并覆盖 属性.
class PropertyBase { }
class PropA : PropertyBase {}
class PropB : PropertyBase {}
class ControllerBase {
var prop: PropertyBase?
}
class ControllerA : ControllerBase{
override var prop: PropA?
}
class ControllerB : ControllerBase{
override var prop: PropB?
}
错误:
Cannot override mutable property 'prop' of type 'PropertyBase?' with covariant type 'PropA?
想知道我是如何用不同的方法实现这一目标的吗?
编辑
我想添加到这个问题中,以更清楚地说明我想要实现的目标。
在示例中,我正在构建一个处理未知对象类型的协议,我所知道的是该类型可能是 String
或 Int
或完全不同的 class Resource
。我想通过添加扩展来支持这些不同类型的 classes。至少那是我认为正确的方法,但是
public protocol InstrumentProtocol : class {
associatedtype Item: AnyObject
var item: Item? { get set }
var identifier: String? { get }
}
public class AbstractBase : InstrumentProtocol {
public var item: AnyObject?
public var identifier: String?
public init(_ i : AnyObject) {
item = i
}
public func about() {
print(self.item) // Any Object
}
}
//// This is what I want to achieve, but it doesn't work
public extension InstrumentProtocol where Self.Item : Instrument {
public func about() {
print(self.item) //Should be of Instrument type
}
}
public extension InstrumentProtocol where Self.Item : String {
public func about() {
print(self.item) //Should be of String type
}
}
item
属性 类型我不知道。在这种情况下,这将是最好的方法吗?
你可以这样做:
class PropertyBase { }
class PropA : PropertyBase {}
class PropB : PropertyBase {}
protocol ControllerBaseType {
associatedtype T: PropertyBase
var prop : T? { get set }
}
class ControllerA : ControllerBaseType {
var prop: PropA?
}
class ControllerB : ControllerBaseType {
var prop: PropB?
}
ControllerBaseType
是你想要的抽象,你在每个子 class
中都有 prop
的具体实现
编辑:
根据@Honey 的评论,我通过从 subclasses
中删除类型别名来简化代码
编辑2:
如果你真的需要 ControllerBase
作为 class 你可以这样做:
class ControllerBase<T: PropertyBase> {
var prop : T?
}
class PropertyBase { }
class PropA : PropertyBase {}
class PropB : PropertyBase {}
class ControllerA : ControllerBase<PropA> {}
class ControllerB : ControllerBase<PropB> {}
有了 Swift 协议、扩展和约束,我想做两件事:
- 创建基础摘要class
Subclass 并覆盖 属性.
class PropertyBase { } class PropA : PropertyBase {} class PropB : PropertyBase {} class ControllerBase { var prop: PropertyBase? } class ControllerA : ControllerBase{ override var prop: PropA? } class ControllerB : ControllerBase{ override var prop: PropB? }
错误:
Cannot override mutable property 'prop' of type 'PropertyBase?' with covariant type 'PropA?
想知道我是如何用不同的方法实现这一目标的吗?
编辑
我想添加到这个问题中,以更清楚地说明我想要实现的目标。
在示例中,我正在构建一个处理未知对象类型的协议,我所知道的是该类型可能是 String
或 Int
或完全不同的 class Resource
。我想通过添加扩展来支持这些不同类型的 classes。至少那是我认为正确的方法,但是
public protocol InstrumentProtocol : class {
associatedtype Item: AnyObject
var item: Item? { get set }
var identifier: String? { get }
}
public class AbstractBase : InstrumentProtocol {
public var item: AnyObject?
public var identifier: String?
public init(_ i : AnyObject) {
item = i
}
public func about() {
print(self.item) // Any Object
}
}
//// This is what I want to achieve, but it doesn't work
public extension InstrumentProtocol where Self.Item : Instrument {
public func about() {
print(self.item) //Should be of Instrument type
}
}
public extension InstrumentProtocol where Self.Item : String {
public func about() {
print(self.item) //Should be of String type
}
}
item
属性 类型我不知道。在这种情况下,这将是最好的方法吗?
你可以这样做:
class PropertyBase { }
class PropA : PropertyBase {}
class PropB : PropertyBase {}
protocol ControllerBaseType {
associatedtype T: PropertyBase
var prop : T? { get set }
}
class ControllerA : ControllerBaseType {
var prop: PropA?
}
class ControllerB : ControllerBaseType {
var prop: PropB?
}
ControllerBaseType
是你想要的抽象,你在每个子 class
prop
的具体实现
编辑: 根据@Honey 的评论,我通过从 subclasses
中删除类型别名来简化代码编辑2:
如果你真的需要 ControllerBase
作为 class 你可以这样做:
class ControllerBase<T: PropertyBase> {
var prop : T?
}
class PropertyBase { }
class PropA : PropertyBase {}
class PropB : PropertyBase {}
class ControllerA : ControllerBase<PropA> {}
class ControllerB : ControllerBase<PropB> {}