Objective-C - Overriding method in subclass

前端 未结 5 1435
借酒劲吻你
借酒劲吻你 2021-01-07 17:20

I am having some trouble figuring out hour to accurately override a method in one of my subclasses.

I have subclass (ClassB) of another customclass (ClassA):

5条回答
  •  自闭症患者
    2021-01-07 18:00

    First, make sure your init method creates a ClassB object and not a ClassA (or something else) object.

    Then, if you want to create a completely different classB (void)methodName: method than the one found in classA, this is the way to go:

    Super is the superclass. By calling [super methodName] you're asking ClassA to execute it's own methodName. If you want to completely override methodName from classA, just don't call super.

    So, basically, in your classB's implementation of methodName:

    -(void)methodName {
      // Remove [super methodName]
      // Insert the code you want for methodName in ClassB
    }
    

    Feel free to read Messages to self and super in Apple's The Objective-C Programming Language document.

提交回复
热议问题