简体   繁体   English

iPhone-在子类上具有超类调用方法…(?)

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

I have several subclasses that derive from one superclass. 我有几个从一个超类派生的子类。 Lets call this superclass "classA". 让我们将此超类称为“ 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. classA的所有子类都有一个称为doStuff:(id)sender的方法,每个类中的每个方法都执行与该类相关的操作。

The problem is that I have to call doStuff from classA. 问题是我必须从classA调用doStuff。

How do I do that? 我怎么做?

Should I declare doStuff:(id)sender on classA as an empty method, like 我应该在classA上将doStuff:(id)sender声明为空方法,例如

- (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. 否则它也可能引发异常,因此您不要忘记提供实现,但是通常它足以在子类需要提供实现的标头中提供文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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