/*Bank结构体管理一个虚拟货币的流通,在这个流通中Bank永远不可能拥有超过 10,000 的硬币在这个游戏中有且只能有一个Bank存在,因此Bank由带有静态属性和静态方法的结构体实现,从而存储和管理其当前的状态。*/structBank{staticvarcoinsInBank=10_000// 从银行中获取钱,获取的钱不能超过10000staticfuncvendCoins(varnumberOfCoinsToVend:Int)->Int{numberOfCoinsToVend=min(numberOfCoinsToVend,coinsInBank)coinsInBank-=numberOfCoinsToVendreturnnumberOfCoinsToVend}// 银行接收钱staticfuncreceiveCoins(coins:Int){coinsInBank+=coins}}classPlayer{varcoinsInPurse:Intinit(coins:Int){coinsInPurse=Bank.vendCoins(coins)}funcwinCoins(coins:Int){coinsInPurse+=Bank.vendCoins(coins)}deinit{//Bank.receiveCoins(coinsInPurse)}}varplayerOne:Player?=Player(coins:100)println("A new player has joined the game with \(playerOne!.coinsInPurse) coins")// 输出 "A new player has joined the game with 100 coins"println("There are now \(Bank.coinsInBank) coins left in the bank")// 输出 "There are now 9900 coins left in the bank"playerOne=nilprintln("PlayerOne has left the game")// 输出 "PlayerOne has left the game"println("The bank now has \(Bank.coinsInBank) coins")// 输出 "The bank now has 10000 coins"