示例工程 10-protocols

10-protocols/Sources/Ch10Demo/App.swift

swift · 1.4 KB

import Ch10Kit
 
@main
struct Ch10Demo {
    static func main() {
        let shapes: [any Shape] = [Circle(radius: 2), Rectangle(width: 3, height: 4)]
        print("[10.1] 名称 = \(ShapeLab.names(shapes))")
        print("[10.1] 总面积 = \(shapes.reduce(0) { $0 + $1.area() })")
 
        print("[10.2] 默认实现(协议类型)= \(DispatchLab.greetThroughProtocol(PlainPerson()))")
        print("[10.2] 具体实现(协议类型)= \(DispatchLab.greetThroughProtocol(CustomPerson()))")
        print("[10.2] 泛型调用 = \(DispatchLab.greetThroughGeneric(CustomPerson()))")
 
        print("[10.3] 具体类型调用 work = \(DispatchLab.workConcrete())")
        print("[10.3] 协议类型调用 work = \(DispatchLab.workThroughProtocol())")
 
        print("[10.4] IntBox 数量 = \(ContainerLab.count(IntBox([1, 2, 3])))")
        print("[10.4] StringBox 拼接 = \(ContainerLab.join(StringBox(["a", "b"])))")
 
        let person = Person(name: "ada", age: 36)
        print("[10.5] 组合 = \(CompositionLab.describe(person))")
        print("[10.5] 名称列表 = \(CompositionLab.names([person]))")
        print("[10.5] String 扩展 = \("abcdef".shortDescription)")
 
        print("[10.6] MemoryLayout<any Shape> = \(MemoryLayout<any Shape>.size) 字节")
        print("[10.6] MemoryLayout<Circle> = \(MemoryLayout<Circle>.size) 字节")
        print("[10.6] MemoryLayout<Person> = \(MemoryLayout<Person>.size) 字节")
    }
}