在《Swift Programming Language》中, 描述了Swift Class实例的初始化调用有经历两个阶段。
结合下面的代码记录之。
class Person {
var name: String
var age: Int
var blog = ""
init(name: String, age: Int) {
self.name = name
self.age = age
}
}
class Programmer: Person {
var preferredSkill: String
override init(name: String, age: Int) {
self.preferredSkill = "Swift"
super.init(name: name, age: age)
self.blog = "CSDN"
}
}
var programmer = Programmer(name: "Ray", age: 24)
println(programmer.preferredSkill)
第一个阶段:
1. 子类Programmer的init()方法被调用
2. Programmer类新的实例的占用内存被分配完毕,但未被初始化(allocated but not initialized)
3. Programmer类初始化由自己引入的stored prope