swift3-19 多态性

多态性 同样的操作或者方法,不同的对象执行的时候表现出完全不同的行为

示例:

class baseClass{
    var name:String
    var age:Int
    init() {
        self.name = ""
        self.age = 0
    }
    init(name:String,age:Int) {
        self.age = age
        self.name = name
    }
    func testFunc() {
        print("baseClass")
    }
}

class sonClass1:baseClass{
    var str1:String = "str1"
    //重载
    override func testFunc() {
        print("sonClass1")
    }
}

class sonClass2:baseClass{
    var str1:String = "str1"
    override func testFunc() {
        print("sonClass2")
    }
}


final class grandSon1:sonClass1{
    var strGrand:String = "strGrand"
}
final class grandSon01:sonClass1{
    var strGrand:String = "strGrand"
}

final class grandSon02:sonClass2{
    var strGrand:String = "strGrand"
}

func printBasicInfo(base:baseClass){
    print("name is \(base.name)")
}

var grandson1 = grandSon01.init(name: "a", age: 10)
var grandson2 = grandSon02.init(name: "b", age: 7)
printBasicInfo(base: grandson1)
printBasicInfo(base: grandson2)
grandson1.testFunc()
grandson2.testFunc()