Call subclass's method from its superclass

前端 未结 6 2162
囚心锁ツ
囚心锁ツ 2020-12-03 12:01

I have two classes, named Parent and Child, as below. Parent is the superclass of Child I can call a method of the super

6条回答
  •  北荒
    北荒 (楼主)
    2020-12-03 12:15

    You could use this:

    Parent.m

    #import "Parent.h"
    
    @implementation Parent
    
    - (void) methodOfChild {
    
        // this should be override by child classes
        NSAssert(NO, @"This is an abstract method and should be overridden");    
    }
    
    @end
    

    The parent knows about the child and child has a choice on how to implement the function.

提交回复
热议问题