Objective-C - Overriding method in subclass

前端 未结 5 1407
借酒劲吻你
借酒劲吻你 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 17:48

    By writing:

    -(void)methodName {
      [super methodName];
    }
    

    You tell the compiler: When executing methodName of Class B, call methodName of its superclass (Class A). So if you want Class B to do something different you have to write code that results in a different behavior. Like this:

    -(void)methodName {
      NSLog(@"Hello, world!");
    }
    

    Now by calling methodName of Class B "Hello, world!" will be printed on the console.

提交回复
热议问题