对未知类的对象调用类方法 [英] Call class method on object of unknown class

查看:197
本文介绍了对未知类的对象调用类方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我向数组添加了一组类,我知道它们具有相同的超类:

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.

这在Objective-C中是不可能的;没有强类型化的类指针。 类类是最好的。

This not possible in Objective-C; there are no strongly typed class pointers. Class class is the best you can do.

您可以将任何已知的类方法发送到 Class 类型变量...

You can send any known class method to a Class typed variable...

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

...无需让编译器抱怨。当然,一些示例可能会在运行时失败。

... without making the compiler complain. Of course some of the examples might fail at runtime.

这篇关于对未知类的对象调用类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆