简体   繁体   中英

iPhone - having a superclass calling methods on subclasses…(?)

I have several subclasses that derive from one superclass. Lets call this superclass "classA".

All subclasses of classA have a method called doStuff:(id)sender and every of these methods in each class does things relative to that class.

The problem is that I have to call doStuff from classA.

How do I do that?

Should I declare doStuff:(id)sender on classA as an empty method, like

- (void) doStuff:(id)sender {
}

just to have it compile correctly and then overwrite that on the subclass declaring it again? Is this the correct way to do that?

thanks.

That is a good way for that case, just have the superclass implement a default method that does nothing. Or it can also throw an exception so you don't forget to provide an implementation, but usually it's good enough to document in the header that subclasses need to provide an implementation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM