简体   繁体   English

在未知类的对象上调用类方法

[英]Call class method on object of unknown class

I added a set of classes to an array, all which I know have the same superclass: 我向数组添加了一组类,我都知道它们具有相同的超类:

[array addObject:[Africa class]];
[array addObject:[Brazil class]];
[array addObject:[France class]];

Later, I want to get the class object and call a superclass class method on it. 稍后,我想获取类对象并在其上调用超类类方法。 Something like this: 像这样:

Class class = [array objectAtIndex:1];

(Country class) specificClass = class;

I've tried a variation of different ideas, but can't figure out how to put that last line in code. 我尝试了各种不同的想法,但无法弄清楚如何将最后一行放在代码中。

If I get you right you want a variable pointing to a class object, statically typed to a concrete class. 如果我理解正确,则需要一个指向类对象的变量,该变量静态键入具体类。

This not possible in Objective-C; 这在Objective-C中是不可能的; there are no strongly typed class pointers. 没有强类型的类指针。 Class class is the best you can do. Class class最好。

You can send any known class method to a Class typed variable... 您可以将任何已知的类方法发送到Class类型变量。

[class alloc];
[class defaultManager];
[class myCommonClassMethod];

... without making the compiler complain. ...而不会令编译器抱怨。 Of course some of the examples might fail at runtime. 当然,某些示例在运行时可能会失败。

With a proper design pattern, you shouldn't need to know which country you are working with. 有了正确的设计模式,您就不需要知道与哪个国家/地区合作。

Start by creating an AbstractCountry class that declares (and provides stub implementations) of all the methods your countries need to generically respond to (ie countries can be more specific). 首先创建一个AbstractCountry类,该类声明(并提供存根实现)您所在国家/地区通常需要响应的所有方法(即,国家/地区可能更具体)。

Then subclass that AbstractCountry for each individual country. 然后将每个AbstractCountry国家的AbstractCountry子类化。

Then: 然后:

AbstractCountry *countryClass = [array objectAtIndex:n];

If you need behavior that only exists on a single country, then either push a stub implementation up (which wouldn't be terribly elegant) or test for response to selector (also not elegant) or cast appropriately (fragile). 如果您需要仅在一个国家/地区存在的行为,则可以提高存根实现(这不会非常优雅),或者测试对选择器的响应(也不很优雅)或进行适当的转换(脆弱)。

Of course, all of this begs the question of why you have classes for this and not instances (though the instance design would be the same; consider something like UIControl and all the subclasses -- the control provides the abstract behavior of controls whereas the subclasses implement specific kinds of controls by oft overriding the abstract methods). 当然,所有这些都引出了一个问题,即为什么要为此类而不是实例创建类(尽管实例设计是相同的;请考虑类似UIControl和所有子类的类-控件提供了控件的抽象行为,而子类通过经常覆盖抽象方法来实现特定类型的控件)。

Usually in Objective-C you would call a method like so: 通常在Objective-C中,您将调用如下方法:

[class someMethod];

So in your case, let's pretend that your superclass (I'm assuming "Country") has a method to set the population or something, it would look like: 因此,在您的情况下,我们假设您的超类(我假设“国家/地区”)具有一种设置人口或其他方法的方法,如下所示:

[Africa setPopulation:someInteger];

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

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