Call subclass's method from its superclass

前端 未结 6 2147
囚心锁ツ
囚心锁ツ 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:09

    You can, as Objective C method dispatch is all dynamic. Just call it with [self methodOfChild], which will probably generate a compiler warning (which you can silence by casting self to id).

    But, for the love of goodness, don't do it. Parents are supposed to provide for their children, not the children for their parents. A parent knowing about a sub-classes new methods is a huge design issue, creating a strong coupling the wrong way up the inheritance chain. If the parent needs it, why isn't it a method on the parent?

提交回复
热议问题