- 默认
class Student {
var name:String = ""
var number:Int = 0
}
var stu1 = Student()
2 .自定义构造函数
class Person {
var name:String
var age:Int
init(newName:String,newAge:Int) {
self.name = newName
self.age = newAge
}
func say() -> String{
return "我是\(self.name)"
}
}
var p = Person(newName: "吴青峰", newAge: 20)
print(p.say())
- 类的反初始化
class DBClass{
var db:String = "zijunfu"
deinit {
print("当前类被销毁时调用的方法")
}
}
var db:DBClass? = DBClass()
db = nil