swift3-20 构造过程

构造过程是为了使用某个类、结构体或者枚举类型实例而进行的准备过程。这个过程包含了为实例的每一个属性设置初始值 的初始化任务
swift的构造函数使用的是init()方法。 这个方法没有返回值。主要任务是保证新实例在第一次使用之前完成正确的初始化。

构造器 在创建某特定类型的新实例时调用,它的关键字是init

init(){
    //执行属性初值设置
}

两段式构造

class baseClass{
    var name:String
    var age:Int
    var level:String?
    init() {
        self.name = ""
        self.age = 0
    }
    init(name:String,age:Int) {
        self.age = age
        self.name = name
    }
    func testFunc() {
        print("baseClass")
    }
}

class sonClass1:baseClass{
    var str1:String

    init(name:String, age:Int, str1:String){
        //构造初值  第一段
        self.str1 = str1
        super.init(name: name, age: age)
        //进一步完善  第二段
        self.agefomat(age: age)

    }
    final func agefomat(age:Int){
        if age <= 0 {
            self.age = 1
        }
    }
    //重载
    override func testFunc() {
        print("sonClass1")
    }
}
let son = sonClass1.init(name: "a", age: 2, str1: "str")

默认属性值

class simple{
    var name = ""
    var age:Int
    init(){
        age = 18
    }
}

构造参数 定义构造器 init() 时提供构造参数

class baseClass{
    var name:String
    var age:Int
    init() {
        self.name = ""
        self.age = 0
    }
    init(name:String,age:Int) {
        self.age = age
        self.name = name
    }
    func testFunc() {
        print("baseClass")
    }
}

可选属性类型

class baseClass{
    var name:String
    var age:Int
    var level:String?
    init() {
        self.name = ""
        self.age = 0
    }
    init(name:String,age:Int) {
        self.age = age
        self.name = name
    }
    func testFunc() {
        print("baseClass")
    }
}

默认构造器 所有属性都设置有默认值的实例

class simple{
    var name = ""
    var age = 0
}

required 构造函数

必须被子类实现的构造函数

class baseClass{
    var name:String
    var age:Int
    var level:String?
    init() {
        self.name = ""
        self.age = 0
    }
    //子类必须实现
    required init(name:String,age:Int) {
        self.age = age
        self.name = name
    }
    func testFunc() {
        print("baseClass")
    }
}

class sonClass1:baseClass{
    var str1:String

    init(name:String, age:Int, str1:String){
        //构造初值
        self.str1 = str1
        super.init(name: name, age: age)
        //进一步完善
        self.agefomat(age: age)

    }
    private final func agefomat(age:Int){
        if age <= 0 {
            self.age = 1
        }
    }
    //子类实现父类的required
    required convenience init(name: String, age: Int) {
        self.init(name: name, age: age, str1: "")
    }
    //重载
    override func testFunc() {
        print("sonClass1")
    }
}
let son = sonClass1.init(name: "a", age: 2, str1: "str")
son.name
son.age
son.level

构造函数的继承

class baseClass{
    var name:String
    var age:Int
    var level:String?
    init() {
        self.name = ""
        self.age = 0
    }
    init(name:String,age:Int) {
        self.age = age
        self.name = name
    }
    func testFunc() {
        print("baseClass")
    }
}

class sonClass1:baseClass{
    var str1:String

    init(name:String, age:Int, str1:String){
        //构造初值
        self.str1 = str1
        super.init(name: name, age: age)
        //进一步完善
        self.agefomat(age: age)

    }
    final func agefomat(age:Int){
        if age <= 0 {
            self.age = 1
        }
    }
   //构造函数的继承
    override convenience init(name: String, age: Int) {
        self.init(name: name, age: age, str1: "")
    }
    //重载
    override func testFunc() {
        print("sonClass1")
    }
}
let son = sonClass1.init(name: "a", age: 2, str1: "str")