Overriding a stored property in Swift

前端 未结 10 806
鱼传尺愫
鱼传尺愫 2020-11-30 00:10

I noticed that the compiler won\'t let me override a stored property with another stored value (which seems odd):

class Jedi {
    var lightSaberColor = \"Bl         


        
10条回答
  •  鱼传尺愫
    2020-11-30 00:42

    In Swift, this is unfortunately not possible to do. The best alternative is the following:

    class Jedi {
        private(set) var lightsaberColor = "Blue"
    }
    
    
    class Sith: Jedi {
        override var lightsaberColor : String {
            get {
                return "Red"
            }
        }
    }
    

提交回复
热议问题