示例工程 09-classes-inheritance-init

09-classes-inheritance-init/Diagnostics/SelfBeforeSuper.swift

swift · 0.3 KB

// 故意编译失败:super.init 之前不能使用 self。
// 复现:swiftc Diagnostics/SelfBeforeSuper.swift
class Base {
    init(x: Int) {}
}
 
class Sub: Base {
    let value: Int
 
    init() {
        value = 1
        print(describe())
        super.init(x: value)
    }
 
    func describe() -> String { "value=\(value)" }
}
 
print(Sub())