示例工程 09-classes-inheritance-init

09-classes-inheritance-init/Sources/Ch09Demo/App.swift

swift · 1.4 KB

import Ch09Kit
 
@main
struct Ch09Demo {
    static func main() {
        let log = LifecycleLog()
        let vehicle = Vehicle(wheels: 4, log: log)
        let car = Car(brand: "A", log: log)
        print("[9.1] vehicle = \(vehicle.description)")
        print("[9.1] car = \(car.description)")
        print("[9.1] 初始化顺序 = \(log.entries)")
 
        let convenience = Car(brand: "B")
        print("[9.2] 便利初始化 = \(convenience.description)")
 
        let vehicles: [Vehicle] = [Vehicle(wheels: 6), Car(brand: "C")]
        print("[9.3] 动态派发 = \(DispatchLab.describeAll(vehicles))")
        print("[9.3] 运行时类型 = \(DispatchLab.typeNames(vehicles))")
 
        let shared = ReferenceLab.sharedMutation()
        print("[9.4] 引用共享 = \(shared.first)/\(shared.second)")
 
        let lifecycle = LifecycleLog()
        weak var weakToken: Token?
        func runScope() {
            let token = Token(name: "scoped", log: lifecycle)
            weakToken = token
            print("[9.5] 作用域内 = \(lifecycle.entries)")
        }
        runScope()
        print("[9.5] 作用域外 = \(lifecycle.entries),weak 为 nil = \(weakToken == nil)")
 
        print("[9.6] MemoryLayout<Vehicle> = \(MemoryLayout<Vehicle>.size) 字节")
        print("[9.6] MemoryLayout<Car> = \(MemoryLayout<Car>.size) 字节")
        print("[9.6] MemoryLayout<Counter> = \(MemoryLayout<Counter>.size) 字节")
    }
}