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

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-11
  • 2022-12-23
  • 2022-01-10
猜你喜欢
  • 2021-10-02
  • 2021-12-16
  • 2021-10-21
  • 2021-07-15
  • 2021-04-21
  • 2022-12-23
相关资源
相似解决方案