Hello everyone,
I got a question and here is a little example to demonstrate it.
Lets suppose I got a class Soldier which has some typical attributes.
class Soldier {
var healthPoints: Int
var attack: Int
var defense: Int
var type: SoldierType
...
}
enum SoldierType {
case ARCHER
case INFANTRY
case CAVALARY
}
Now I want to use several soldier types with different default values therefore I have two ideas:
1. Subclassing the Soldier class and then providing an initializer for each type of soldier I want.
e.g.
class Archer: Soldier {
init() {
super.init()
healtPoints = 100
attack = 10
defense = 2
type = .ARCHER
}
}
...
2. My second approach would be to initialize the Soldier with a type and then switch the type.
init(type: SoldierType) {
self.type = type
switch type {
case .ARCHER:
healtPoints = 100
attack = 10
defense = 2
...
}
I hope Iam not on a completly wrong path.
If not, I would prefer the first option,
but since Swift has no access modifier for the class and subclasses only, I would have to make everything private and provide setter, which I really do not like.
If there are other better ways, I would be glad if you let me know.
Thanks in advance
Chris