开发者

Objective C - Overriding a base class method - How to call subclass's method?

I have a base class which is used extensively in my code, but when I subclass it the base class method gets called in the following example code:

SubClass *foo;
[array addObject:foo];

for ( BaseClass 开发者_开发问答*bc in array ) {
  [bc foo];
}

Is there a way to get the subclass's foo method called without changing the for loop? Can I do something like

foo {
  if ( what_class_am_i_really != BaseClass ) [what_class_am_i_really foo];
}


Objective-C will automatically call the implementation of the subclass.

If it doesn't do this in your case, there's a bug somewhere in your code, and you need to show those methods.


The subclass's method is called automatically. That's polymorphism. Casting it to the superclass does not invoke the superclass method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜