覆盖子类Swift中的静态var

覆盖子类Swift中的静态var,第1张

概述为了分解我的代码,我想从一个Child类中设置一个stringKey,我将在父类的类func中得到它: Chair.request();Table.request();class Furniture: NSObject { static let requestKey : String! class func request(){ print("class 为了分解我的代码,我想从一个Child类中设置一个stringKey,我将在父类的类func中得到它:

Chair.request();table.request();class Furniture: NSObject {    static let requestKey : String!    class func request(){        print("class key => \(self.requestKey)")        ...    }}class Chair: Furniture {    static let requestKey : String = "Jsonchairs"} class table: Furniture {    static let requestKey : String = "tables"}

当然,我有预编译的错误消息

Property does not overrIDe any property from its superclass

有没有解决方案,或者我需要传递密钥作为参数?像这样:

Chair.request(key : "Jsonchairs" );table.request(key : "tables" );
解决方法 刚遇到同样的问题.使用计算属性 – 可以覆盖这些属性.

基类:

class BaseLanguage {    class var language: String {        return "Base"    }    static func getLocalized(key: String) -> String {        print("language: \(language)");    }}

儿童班:

class German: BaseLanguage {    overrIDe class var language: String {        return "de"    }}

如果由于某种原因无法使用计算属性,则始终可以将变量属性包装在私有单例中.该类将外部接口公开为静态,但在其内部具有对其实例的静态私有引用.您可以在init方法中更改任何属性的值(只要它是变量).

总结

以上是内存溢出为你收集整理的覆盖子类Swift中的静态var全部内容,希望文章能够帮你解决覆盖子类Swift中的静态var所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1007230.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-22
下一篇2022-05-22

发表评论

登录后才能评论

评论列表(0条)

    保存