在实例被回收前调用
析构函数如何工作 (How Deinitialization Works)
Swift 利用"自动引用计数 (Automatic reference counting)" 自动回收不再使用的实例
手动执行一些清理:
deinit{
}
最多只能有一个
超类的解初始化和被自动继承, 如果子类有实现会自动在实现的最后被调用
解初始化实际使用 (Deinitializers in Action)
银行取钱和存钱:
class Bank {
static var coinsInBank = 10_000
static func distribute(coins numberOfCoinsRequested: Int) -> Int {
let numberOfCoinsToVend = min(numberOfCoinsRequested, coinsInBank)
coinsInBank -= numberOfCoinsToVend
return numberOfCoinsToVend
}
static func receive(coins: Int) {
coinsInBank += coins
}
}
赌徒取钱和赢钱:
class Player {
var coinsInPurse: Int
init(coins: Int) {
coinsInPurse = Bank.distribute(coins: coins)
}
func win(coins: Int) {
coinsInPurse += Bank.distribute(coins: coins)
}
deinit {
Bank.receive(coins: coinsInPurse)
}
}
最后将钱返回给银行
测试1:
var playerOne: Player? = Player(coins: 100)
print("A new player has joined the game with \(playerOne!.coinsInPurse) coins")
print("There are now \(Bank.coinsInBank) coins left in the bank")
测试2:
playerOne!.win(coins: 2_000)
print("PlayerOne won 2000 coins & now has \(playerOne!.coinsInPurse) coins")
print("The bank now only has \(Bank.coinsInBank) coins left")
测试3:
playerOne = nil
print("PlayerOne has left the game")
print("The bank now has \(Bank.coinsInBank) coins")
❤️ 感谢你的访问,欢迎留言交流!❤️