有人可以幫我理解這個Swift代碼嗎?此代碼是根據預定我最近在讀的原型設計模式的實現:Swift原型設計模式
class AbstractCard {
var name: String?
var mana: Int?
var attack: Int?
var defense: Int?
init(name:String?, mana:Int?, attack:Int?, defense:Int?) {
self.name = name
self.attack = attack
self.defense = defense
self.mana = mana
}
func clone() -> AbstractCard {
return AbstractCard(name: self.name, mana: self.mana, attack:
self.attack, defense: self.defense)
}
}
class Card: AbstractCard {
var someNumber: Int
override init(name:String?, mana:Int?, attack:Int?, defense:Int?){
someNumber = 2
super.init(name: name,mana: mana,attack: attack,defense:
defense)
}
}
// Simulate our client
// This is the card that we will copy
let raidLeader = Card(name: "Raid Leader", mana: 3, attack: 2, defense: 2)
// Now we use our faceless Manipulator card to clone the
let facelessManipulator = raidLeader.clone()
print("\(facelessManipulator.name, facelessManipulator.mana, facelessManipulator.attack, facelessManipulator.defense)")
print(facelessManipulator.dynamicType) // prints "AbstractCard"
這是什麼模式的點,如果克隆的對象的dynamicType仍然AbstractCard,而不是卡。你甚至不能訪問特定於卡的變量。我試圖施放此對象卡,但我得到的錯誤"Execution was interrupted reason: signal SIGABRT"
我想你需要重寫'卡'中的克隆。 – tktsubota
'AbstractCard'中'clone'的實現有點毫無意義,那不是嗎?因爲這只是一個抽象類。 – ttrs