从该类中调用一个类方法 [英] Call a class method from within that class

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

问题描述

是否可以从同一个类中的另一个方法中调用一个类方法?

Is there a way to call a class method from another method within the same class?

例如:

+classMethodA{
}

+classMethodB{
    //I would like to call classMethodA here
}

推荐答案

在类方法中,self表示要发送消息的类.因此,从另一个类方法(例如classMethodB)中,使用:

In a class method, self refers to the class being messaged. So from within another class method (say classMethodB), use:

+ (void)classMethodB
{
    // ...
    [self classMethodA];
    // ...
}

在实例方法(例如instanceMethodB)中,使用:

From within an instance method (say instanceMethodB), use:

- (void)instanceMethodB
{
    // ...
    [[self class] classMethodA];
    // ...
}

请注意,两个都不会假定您正在发送消息.实际的类可能是子类.

Note that neither presumes which class you are messaging. The actual class may be a subclass.

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

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