如何从类的实例调用方法?

问题描述:

-(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.