Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swift的析构 #1

Open
miss-shiyi opened this issue Sep 27, 2021 · 0 comments
Open

Swift的析构 #1

miss-shiyi opened this issue Sep 27, 2021 · 0 comments
Assignees
Labels
Swift Extra attention is needed

Comments

@miss-shiyi
Copy link
Owner

1. 析构就是在一个实例在被内存释放前调用的一个函数

与初始化使用关键字“init”类似。析构我们使用关键字“deinit”,并且,只有类(引用类型)才可以使用析构。

  • ·只有类可以使用析构函数。
  • ·每个类最多有且只有一个析构函数。
  • ·析构函数不带任何参数且没有括号。
  • ·析构函数在实例被内存释放前调用。
  • ·析构函数不能被别人手动调用。
  • ·先调用子类的析构函数,再调用父类的析构函数。
  • ·子类必定会调用父类的析构函数。
struct 老虎机 { 

static var 银币 = 10_000 
static func 赢一场( 赢的游戏币: Int)->Int{ 

var 赢游戏币 = 赢的游戏币 
赢游戏币 = min(赢游戏币 ,银币) 
银币 -= 赢游戏币 
return 赢游戏币
 } 

static func 输光(输游戏币:Int){ 

银币 += 输游戏币

 }
} 

class 玩家 { var 游戏币: Int

init(游戏币: Int){ 

self.游戏币 = 游戏币 } 

func 赢取游戏(赢游戏币: Int){ 

游戏币 += 老虎机.赢一场(赢游戏币: 赢游戏币)
 } 

deinit{ 老虎机.输光(输游戏币: 游戏币) } 

} 

@miss-shiyi miss-shiyi self-assigned this Sep 27, 2021
@miss-shiyi miss-shiyi added the Swift Extra attention is needed label Sep 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Swift Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant