Swift基础05
构造器(构造函数)
类的构造过程
- 类的构造过程就是给我们的类分配内存空间,对我们的类的存储属性赋值,生成我们想要的对象的过程
- 对存储属性赋值:可以在定义属性的时候,就给它赋初值。
- 如果没有赋初值,就必须在我们的构造函数(init方法)里面赋值,否则会报错
class Person: NSObject {
var name: String = "xiaowang"
var age: Int = 20
}
class Man: Person {
var weight: Float
var height: Float
init (weight: Float, height: Float) {
self.weight = weight
self.height = height
}
}
构造器(构造函数)
- 所有的构造器的名字都叫 init
- 构造器的功能是用来完成类的构造过程。生成我们需要对象
- 如果我们的所有的存储属性都已经被初始化完成。那么,类会给我们创建一个默认的构造器 init()
- 如果我们属性没有初始化完成,就需要我们自己去定义一个构造器
class Person: NSObject {
var name: String
var age: Int
init (name: String, age: Int) {
self.name = name
self.age = age
}
}
let person0 = Person.init(name: "xiaowang", age: 20)
- 以下四种属性在构造对象过程中,不需要考虑它们是否已被初始化
class Person {
var name: String = "xiaowagn"
var kindName: String {
get {
return "亲爱的\(name)"
}
}
var girlFriend: String?
lazy var pet: String = {
return "xiaobao"
}()
}
便利构造器
- 便利构造器,就是简化类的构造过程的构造器
- 用 convenience 关键字来修饰
class Person: NSObject {
var name: String
var age: Int
var country: String
var love: String
var girlFriend: String
init (name: String, age: Int, country:String, love: String, girlFriend: String) {
self.name = name
self.age = age
self.country = country
self.love = love
self.girlFriend = girlFriend
}
convenience override init(){
self.init(name: "宝宝", age: 0, country: "china", love: "cry", girlFriend: "")
}
}
let baby = Person(name: "宝宝", age: 0, country: "china", love: "cry", girlFriend: "")
let baby2 = Person()
print("name: \(baby2.name), age:\(baby2.age)")
let button = UIButton(type: .ContactAdd)
KVC构造器
- kvc 的原理是在运行时,动态地调用 setValue(value: AnyObject?, forKey key: String)
- kvc 和可选的基本数据类型属性不兼容,记住!是可选的!
- 如果属性是基本的数据类型,我们需要将其设为必须属性,并赋初值.
class Person: NSObject{
var name: String?
var age: Int = 0
init (dict: [String: NSObject]){
super.init()
setValuesForKeysWithDictionary(dict)
}
override func setValue(value: AnyObject?, forKey key: String) {
super.setValue(value, forKey: key)
}
override func setValue(value: AnyObject?, forUndefinedKey key: String) {
print("key:\(key), value:\(value)")
}
}
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let person = Person(dict:["name":"xiaowang", "age": 20, "abc":"456"])
print("name: \(person.name), age: \(person.age)")
}
}
可失败构造器
- 构造器没有按照预期成功构造对象时,可令其返回nil
- 可失败构造器,在返回 nil 之前,也必须保证所有属性初始化
class Person: NSObject {
var name: String
var age: Int
init?(name: String, age: Int) {
self.name = name
self.age = age
super.init()
if age > 200 {
return nil
}
}
}
let person = Person(name: "xiaowang", age: 300)
print(person?.name)
print(person?.age)
构造器的继承规则
- 如果子类没有实现任何父类的指定构造器,则继承所有的父类的指定构造器和便利构造器
- 如果子类重写了所有的父类的指定构造器,则默认继承父类所有便利构造器
- 如果没有全部重写父类的指定构造器,则父类的构造器一个也不继承
class Person:NSObject {
var name: String
var age: Int
init(name: String, age: Int) {
self.name = name
self.age = age
}
init(name: String) {
self.name = name
self.age = 25
}
convenience override init() {
self.init(name: "宝宝", age: 0)
}
}
class Man: Person{
}
let man = Man()
print(man.name)
print(man.age)
let man1 = Man(name: "xiaoming")
print(man1.name)
print(man1.age)
let man2 = Man(name: "xiaohua", age: 20)
print(man2.name)
print(man2.age)
class Person:NSObject {
var name: String
var age: Int
init(name: String, age: Int) {
self.name = name
self.age = age
}
init(name: String) {
self.name = name
self.age = 25
}
convenience override init() {
self.init(name: "宝宝", age: 0)
}
}
class Man2: Person {
override init(name: String) {
super.init(name: name)
}
override init(name: String, age: Int) {
super.init(name: name, age: age)
}
}
let man1 = Man2()
print(man1.name)
print(man1.age)
class Person:NSObject {
var name: String
var age: Int
init(name: String, age: Int) {
self.name = name
self.age = age
}
init(name: String) {
self.name = name
self.age = 25
}
convenience override init() {
self.init(name: "宝宝", age: 0)
}
}
class Man3: Person{
convenience init() {
self.init(name:"xiaoxiao", age:20)
}
override init(name: String, age: Int) {
super.init(name: name, age: age)
}
}
let man1 = Man3()
print(man1.name)
print(man1.age)