如何从类的实例调用方法? [英] How do I call a method from an instance of a class?

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

问题描述

-(NSDictionary *)properties;
+(NSDictionary *)ClassProperties;

现在,我如何从子类调用 ClassProperties?

Now, how can I call ClassProperties from sub-classes?

-(NSDictionary *)properties {
    return [? ClassProperties];
}

重点是 ClassProperties 获取类中的属性列表,所以我不能调用基类定义.

The point is that ClassProperties gets the list of properties in the class, so i can't call the base class definition.

推荐答案

按照 Marc 的回复,您可以更一般地调用该方法

Along the lines of Marc's response, you could more generally call the method with

[[self class] ClassProperties]

事实上,如果你的基类和所有的子类都实现了+ (NSDictionary *)ClassProperties,那么你的基类就可以做到这一点

In fact, if your base class and all the subclasses implement + (NSDictionary *)ClassProperties, then your base class can do this

- (NSDictionary *)properties {
    return [[self class] ClassProperties];
}

然后你的子类都不需要知道- (NSDictionary *)properties.将根据 self 是什么来调用正确的类方法.

and then none of your subclasses will need to know about - (NSDictionary *)properties. The correct class method would be called based on what self is.

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

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