gpt4 book ai didi

ios - Swift 类基本 D.R.Y 原则

转载 作者:行者123 更新时间:2023-11-30 12:16:22 25 4
gpt4 key购买 nike

拥有一个名为 Nike 的类并创建该类的实例是我认为应该避免代码重复的做法。这是最聪明的方法吗?

enum Condition {
case poor
case fair
case excellent
}

class Shoe {

var color: Condition
var laces: Condition

init(color: Condition, laces: Condition) {
self.color = color
self.laces = laces
}

}

class Nikeone: Shoe {

var texture: Condition
var sole: Condition

init(color: Condition, laces: Condition, texture: Condition, sole: Condition) {
self.texture = texture
self.sole = sole
super.init(color: color, laces: laces)
}
}




class NikeTwo: Shoe {

var texture: Condition
var sole: Condition

init(color: Condition, laces: Condition, texture: Condition, sole: Condition) {
self.texture = texture
self.sole = sole
super.init(color: color, laces: laces)
}

}

class NikeThree: Shoe {

var texture: Condition
var sole: Condition

init(color: Condition, laces: Condition, texture: Condition, sole: Condition) {
self.texture = texture
self.sole = sole
super.init(color: color, laces: laces)
}
}

拥有一个名为 Nike 的类并创建该类的实例是我认为应该避免代码重复的做法。这是最聪明的方法吗?

最佳答案

您最好的方法是按照您的建议创建一个名为 Shoe 的基类。

enum Condition {
case poor
case fair
case excellent
}

class Shoe {
var color: Condition
var laces: Condition

init(color: Condition, laces: Condition) {
self.color = color
self.laces = laces
}
}

然后您将创建一个名为 NikeShoe 的子类:

class NikeShoe: Shoe {
var texture: Condition
var sole: Condition

init(color: Condition, laces: Condition, texture: Condition, sole: Condition) {
self.texture = texture
self.sole = sole
super.init(color: color, laces: laces)
}
}

然后您将制作您想要的鞋子:

let shoeA = NikeShoe(…)
let shoeB = NikeShoe(…)

关于ios - Swift 类基本 D.R.Y 原则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45395266/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com