类方法和实例方法之间的区别? [英] Difference between class methods and instance methods?

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

问题描述

当在编程中使用实例方法和类方法时,我总是感到困惑.请告诉我实例方法和类方法之间的区别以及彼此的优点.

I always confusing to when i used of instance method and class method in programming. Please tell me difference between instance method and class methods and advantages of one another.

推荐答案

所有其他答案似乎都已被现已修复的错误标签所捕获.

All the other answers seem to have been caught out by the incorrect tag that has now been fixed.

在Objective-C中,实例方法是在将消息发送到类的实例时调用的方法.因此,例如:

In Objective-C, an instance method is a method that is invoked when a message is sent to an instance of a class. So, for instance:

id foo = [[MyClass alloc] init];
[foo someMethod];
//   ^^^^^^^^^^   This message invokes an instance method.

在Objective-C中,类本身就是对象,而class方法只是将消息发送到class对象时调用的方法.即

In Objective-C, classes are themselves objects and a class method is simply a method that is invoked when a message is sent to a class object. i.e.

[MyClass someMethod];
//       ^^^^^^^^^^   This message invokes a class method.

请注意,在上面的示例中,选择器是相同的,但是因为在一种情况下将选择器发送到MyClass的实例,而在另一种情况下将选择器发送到MyClass,则将调用不同的方法.在接口声明中,您可能会看到:

Note that, in the above examples the selector is the same, but because in one case it is sent to an instance of MyClass and in the other case it is sent to MyClass, different methods are invoked. In the interface declaration, you might see:

@interface MyClass : NSObject
{
}

+(id) someMethod;  // declaration of class method
-(id) someMethod;  // declaration of instance method

@end

以及实施中

@implementation MyClass

+(id) someMethod
{
    // Here self is the class object
}
-(id) someMethod
{
    // here self is an instance of the class
}

@end

修改

对不起,错过了第二部分.这样做没有任何优点或缺点.这就好比问问while和if之间的区别是什么,以及一个相对于另一个的优点是什么.这是毫无意义的,因为它们是为不同目的而设计的.

Sorry, missed out the second part. There are no advantages or disadvantages as such. It would be like asking what is the difference between while and if and what are the advantages of one over the other. It's sort of meaningless because they are designed for different purposes.

类方法最常见的用法是在需要实例时获取实例. +alloc是一个类方法,它为您提供了一个新的未初始化的实例. NSString有大量的类方法可以为您提供新的字符串,例如+ stringWithForma

The most common use of class methods is to obtain an instance when you need one. +alloc is a class method which gives you a new uninitialised instance. NSString has loads of class methods to give you new strings, e.g. +stringWithForma

另一种常见用途是获取单例,例如

Another common use is to obtain a singleton e.g.

+(MyClass*) myUniqueObject
{
    static MyUniqueObject* theObject = nil;
    if (theObject == nil)
    {
        theObject  = [[MyClass alloc] init];
    }
    return theObject;
}

由于theObject是静态的,因此上述方法也可以用作实例方法.但是,如果将其设为类方法并且不必先创建实例,则语义会更清晰.

The above method would also work as an instance method, since theObject is static. However, the semantics are clearer if you make it a class method and you don't have to first create an instance.

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

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