Swift 中的静态方法继承

Base and Derived Classes:

class BaseClass{
    class func staticMethod(){
        println("BaseClass.staticMethod")
    }
    
    class func staticMethodWithSelfCall(){
        self.staticMethod()
    }
    
    func instanceMethodWithStaticCall(){
        self.dynamicType.staticMethod()
    }
}

class DerivedClass : BaseClass{
    override class func staticMethod(){
        println("DerivedClass.staticMethod")
    }
}

Test Code:

    BaseClass.staticMethod()
    DerivedClass.staticMethod()
    
    BaseClass.staticMethodWithSelfCall()
    DerivedClass.staticMethodWithSelfCall()
    
    BaseClass().instanceMethodWithStaticCall()
    DerivedClass().instanceMethodWithStaticCall()

Output:

BaseClass.staticMethod
DerivedClass.staticMethod
BaseClass.staticMethod
DerivedClass.staticMethod
BaseClass.staticMethod
DerivedClass.staticMethod

 

posted @ 2014-11-17 13:31  昝昝  阅读(1119)  评论(0编辑  收藏  举报