这期内容当中小编将会给大家带来有关swift中怎么实现初始化和反初始化,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
//类的初始化器 class Temperature { var number: Double let name: String init () { number = 20.0 name = "abc" } }
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中怎么实现初始化和反初始化了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。