编辑代码

class Vehicle {
    var name: String
    init(name: String) {
        self.name = name
    }
    func run() {
    }
}
class Benz: Vehicle {
    override func run() {
        print("Benz Run")
    }
}
class Bmw: Vehicle {
    override func run() {
        print("Bmw Run")
    }
}
var benz = Benz(name: "Benz C300L")
var bmw = Bmw(name: "Bmw 740Li")
benz.run()
bmw.run()
print(benz is Vehicle)
let v = benz as Vehicle
v.run()
print("=======================")
var vs = [Vehicle]()
vs.append(benz); vs.append(bmw)
for vv in vs {
    vv.run()
}

print("----测试case中进行类型转换------")

var aa = [Any]()
aa.append("linhuadong")
aa.append(12)
aa.append(0.23)
aa.append(benz)

for it in aa {
    switch it {
        case let i as Int:
            print("int value: \(i)")
        case let s as String:
            print("string value: \(s)")
        case let v as Vehicle:
            v.run()
        case let dbl as Double:
            print("double value: \(dbl)")
        default:
            print("unknown value")
    }
}