1
我是Swift和靜態編程的初學者。閱讀大書呆子牧場Swift書。我不明白的是爲什麼myTown人口正在變化,但不是fredTheZombie.town?這次演習要求城鎮人口不要低於零。當人口少於10歲時可能會發生這種情況。如何更改fredTheZombie.town人口變量?Swift - 更改結構中的變量?
struct Town {
var population = 5422
var numberOfStoplights = 4
func printTownDescription() {
print("Population: \(myTown.population), number of stoplights: \(myTown.numberOfStoplights)")
}
mutating func changePopulation(amount: Int) {
population += amount
}
}
class Monster {
var town: Town?
var name = "Monster"
func terrorizeTown() {
if town != nil {
print("\(name) is terrorizing a town!")
} else {
print("\(name) hasn't found a town to terrorize yet...")
}
}
}
class Zombie: Monster {
var walksWithLimp = true
final override func terrorizeTown() {
guard town?.population > 0 else {
return
}
if town?.population > 10 {
super.town!.changePopulation(-10)
} else {
super.town!.population = 0
}
super.terrorizeTown()
}
func changeName(name: String, walksWithLimp: Bool) {
self.name = name
self.walksWithLimp = walksWithLimp
}
}
var myTown = Town()
myTown.changePopulation(500)
let fredTheZombie = Zombie()
fredTheZombie.town = myTown
fredTheZombie.terrorizeTown()
fredTheZombie.town?.printTownDescription()
fredTheZombie.changeName("Fred the Zombie", walksWithLimp: false)
myTown.changePopulation(-5915)
print(myTown.population)
print(fredTheZombie.town!.population)
fredTheZombie.terrorizeTown()
fredTheZombie.terrorizeTown()
fredTheZombie.town?.printTownDescription()
輸出:
Monster is terrorizing a town!
Population: 5922, number of stoplights: 4
7
5912
Fred the Zombie is terrorizing a town!
Fred the Zombie is terrorizing a town!
Population: 7, number of stoplights: 4
Program ended with exit code: 0
這很有道理。有沒有辦法改變fredTheZombie.town而不把它變成同一個實例(myTown)? – ltrainpr
@ltrainpr您可以指定任何變量或調用變異方法。但是,因爲'fredTheZombie.town'是一個可選項,所以它不會真正起作用,因爲解包的結果又是一個副本。處理值類型的最常見方式是始終創建一個新實例。 – Sulthan
@ltrainpr哦,我現在看到你的問題,讓我編輯答案。 – Sulthan