swift中怎么实现初始化和反初始化
这期内容当中小编将会给大家带来有关swift中怎么实现初始化和反初始化,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
网站建设哪家好,找成都创新互联!专注于网页设计、网站建设、微信开发、微信平台小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了璧山免费建站欢迎大家使用!
类的初始化器
//类的初始化器 class Temperature { var number: Double let name: String init () { number = 20.0 name = "abc" } }
便捷初始化器convenience
class Temperature { var number: Double let name: String init () { number = 20.0 name = "abc" } convenience init(fahrenheit: Double) { self.init() number = fahrenheit } convenience init(celsius: Double) { self.init(fahrenheit: celsius) } } var f = Temperature() print(f.number) //20.0
子类初始化器
class Person { var name: String var age: Int init(name: String, age: Int) { self.name = name self.age = age } convenience init() { self.init(name: "na", age: 0) } //反初始化器 deinit { print("exit") } } class Teacher: Person { var salary: Int init(name: String, age: Int, salary: Int) { self.salary = salary super.init(name: name, age: age) self.name = name + "老师" } convenience init(name:String) { self.init(name: name, age: 25, salary: 5000) } func showInfo() { print("teacher name \(name), age \(age), salary \(salary)") } } var teacher: Teacher? = Teacher(name: "Atwal") teacher!.showInfo() //teacher name Atwal老师, age 25, salary 5000 teacher = nil //exit
上述就是小编为大家分享的swift中怎么实现初始化和反初始化了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。
网页标题:swift中怎么实现初始化和反初始化
转载源于:http://scyanting.com/article/iiedpo.html