我遇到了let
变量声明的情况。在基类中初始化。我需要在扩展这个基类的其他类中传递一个不同的枚举。
所以,我尝试在基类中创建一个类函数,以便我可以覆盖并返回不同的枚举类型。但有什么方法可以从基类访问扩展类?
在下面创建了一个示例代码,以帮助解释:
class A {
var string: String {
get {
// Is it possible to refer to the class type dynamically here ?
// So that it would call B's printMessage
return A.printMessage("Hello")
}
}
class func printMessage(message: String) -> String {
return "You shall not pass !"
}
}
class B: A {
override class func printMessage(message:String) -> String {
return message + "World !"
}
}
let obj = B()
print(obj.string)
答案 0 :(得分:2)
像这样:
var string: String {
get {
// Is it possible to refer to the class type dynamically here ?
// So that it would call B's printMessage
return self.dynamicType.printMessage("Hello")
}
}