简体   繁体   English

面向协议的编程和类 Swift

[英]Protocol Oriented Programming and Class Swift

I am trying to implement protocol oriented class watching tutorials from on of the popular tutorial sites.我正在尝试从流行的教程网站上实现面向协议的课程观看教程。 I got everything correct so far but for some reason one of the properties is not being updated while the others are updated.到目前为止,我一切都正确,但由于某种原因,其中一个属性没有更新,而其他属性已更新。 Here is the code :这是代码:

protocol Paintable  : class {
    var primaryBodyColor : String{get set}
    var secondaryBodyColor : [String]? {get set}

    func paint(newPrimaryColor : String, newSecondaryColor : [String]?)
}


extension Paintable   {
    var primaryBodyColor : String {
        get {
            return "Black"
        }
        set {
        }
    }
}

protocol Wheeled {
    var numberOfWheels : Int {get set}
}


extension Wheeled {
    var numberOfWheels : Int {
        get {
            return 4
        }

        set {
        }
    }
}
protocol EngineSize {
    var engineSizeCC : Int {get set}
}

extension EngineSize {
    var engineSizeCC : Int {
        return 2300
    }
}




class Honda : Transport, Paintable, Wheeled, EngineSize {
    var secondaryBodyColor : [String]?
    var engineSizeCC : Int
    //We can also override default initializer of protocol, here we are going to instantiate with our initializer
    init(engineSizeCC : Int){
        self.engineSizeCC = engineSizeCC
    }

    func paint(newPrimaryColor: String, newSecondaryColor: [String]?) {
        primaryBodyColor = newPrimaryColor
        secondaryBodyColor = newSecondaryColor
    }
}

var civic = Honda(engineSizeCC: 400)
civic.primaryBodyColor
civic.passengerCapacity
civic.numberOfWheels
civic.engineSizeCC

civic.paint("blue", newSecondaryColor: ["Red","Purple"])
civic.primaryBodyColor -> "Black"
civic.secondaryColor -> ["Red"], ["Purple"] 

The problem I am having is the primaryBodyColor stays "Black" even though I called the "paint" function to set the primary color as "Blue" and secondary color.我遇到的问题是,primaryBodyColor 保持“黑色”,即使我调用“paint”函数将原色设置为“蓝色”和辅助色。

I am new to protocol oreinted programming so I will appreciate help on resolving this issue.我是协议导向编程的新手,所以我将感谢解决此问题的帮助。

Extensions cannot store properties, which is perhaps why you don't have any code in the setter of primaryBodyColor .扩展不能存储属性,这也许就是为什么在primaryBodyColor的 setter 中没有任何代码的primaryBodyColor The implementation of primaryBodyColor needs to be in a struct/class, just as you've done with seondaryBodyColor . primaryBodyColor的实现需要在结构/类中,就像您对seondaryBodyColor所做的seondaryBodyColor So if just before that line within the Honda class you include the following line, your code will work as expected...因此,如果就在 Honda 类中的该行之前包含以下行,则您的代码将按预期工作......

var primaryBodyColor = "black"

Like it's been said in the comments, your implementation of the primaryBodyColor always return black and doesn't allow to be set.就像评论中所说的那样,您对primaryBodyColor的实现总是返回黑色并且不允许设置。

By default, properties in Swift have a get and set method, there's no need to add {get set} after each one of them.默认情况下,Swift 中的属性有一个getset方法,无需在每个{get set}后添加{get set}

If you want to add a default value, you can do it like this:如果要添加默认值,可以这样做:

var primaryBodyColor = "Black"

You don't even need to declare it as String because the compiler will realize that "Black" is a string and type it correctly.您甚至不需要将其声明为 String,因为编译器会意识到"Black"是一个字符串并正确键入它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM