Can someone help me to understand this Swift code? This code is an implementation of prototype design pattern according to book I'm currently reading:
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"
What's the point of this pattern if dynamicType of cloned object is still AbstractCard and not Card. You can't even access vars specific to Card. I tried to cast this object as Card but I get an error "Execution was interrupted reason: signal SIGABRT"
Aucun commentaire:
Enregistrer un commentaire