Swift-OOP-初始化器

2020-11-24  本文已影响0人  守护地中海的花

required

print("required")
class Person {
    var name: String
    var age: Int
    required init(name: String,age: Int) {
        self.name = name
        self.age = age
    }
}

var person = Person(name: "wpp", age: 27)
print(person.name,person.age)

class Student: Person {
    required init(name: String, age: Int) {
        super.init(name: name, age: age)
    }
}

属性观察器

class Person {
    var age: Int {
        willSet {
            print("willSet",newValue)
        }
        didSet {
            print("didSet",oldValue,age)
        }
    }
    init() {
        age = 0
    }
}

class Student: Person {
    override init() {
        super.init()
        self.age = 1
    }
}

let stu = Student()
print(stu)
上一篇下一篇

猜你喜欢

热点阅读