class Person: NSObject {
var name: String?
var age: Int = 0
//1便利构造函数,允许返回nil
//2本身不负责对象的创建
//3需要在调用self.init()创建对象后,才能访问对象的属性
convenience init?(name: String, age: Int) {
if age > 100 {
return nil
}
self.init()
self.name = name
self.age = age
}
}
原文地址:https://www.cnblogs.com/we-smile/p/9375100.html
时间: 2024-10-23 17:27:54