微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Swift - 类初始化和反初始化方法init与deinit

1,init():类的初始化方法(构造方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/********************************
使用认构造函数
********************************/
class Student {
//类属性
var name: String = ""
number: Int = 0
}
student = ()
/********************************
********************************/
Person {
//类属性
String
age: Int
//类构造函数
init (newName: ,newAge: ){
self .name = newName
.age = newAge
}
//类方法
func say() -> {
return "我叫\(name)"
}
}
p = (newName: "hangge" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,newAge: 32)
print (p.say())

2,deinit():类反初始化方法(析构方法
11
DBClass {
conn: Connection ? = ()
deinit{
//可以做一些清理工作
.conn!.close()
.conn = nil
}
db: ()
db = nil //设置nil后即可执行deinit()方法

原文地址:https://www.jb51.cc/swift/324951.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐