Objective-C中实例方法和类方法的区别是什么? [重复]

时间:2022-09-09 22:29:23

Possible Duplicate:
Objective-C: Class vs Instance Methods?
Objective-C - difference between class method and static method?

可能重复:Objective-C:类与实例方法? Objective-C - 类方法和静态方法的区别?

In ObjC, A single dash before a method name means it's a instance method. A plus before a method name means it's a class method. but what is the difference in programming?

在ObjC中,方法名称前面的单个破折号表示它是一个实例方法。方法名称前面的加号表示它是一个类方法。但编程有什么不同?

3 个解决方案

#1


2  

The difference between a class method and an instance method is that an instance method requires an instance of the class on which it will (generally) operate. The message to invoke an instance method must be sent to an instance of a class.

类方法和实例方法之间的区别在于实例方法需要它将(通常)操作的类的实例。必须将调用实例方法的消息发送到类的实例。

Probably the most common single use of class methods is object factories; messages that you send to a class to create an instance configured according to the parameters you've sent in. For example in Cocoa the NSString class has several class methods named stringWithSomethingOrOther: that will create a new NSString object and hand it back to you.

可能最常见的一类方法是对象工厂;您发送给类的消息,以创建根据您发送的参数配置的实例。例如,在Cocoa中,NSString类有几个名为stringWithSomethingOrOther的类方法:它将创建一个新的NSString对象并将其交还给您。

On the other hand, NSString also has many instance methods - operations which really have no meaning without an actual instance to work with. A commonly-used one might be the length method, which tells you how many characters are in the specific NSString instance to which the message is sent.

另一方面,NSString也有许多实例方法 - 在没有实际实例的情况下实际没有意义的操作。常用的方法可能是length方法,它告诉您发送消息的特定NSString实例中有多少个字符。

Also see this. What is the difference between class and instance methods?

也看到这个。类和实例方法有什么区别?

#2


0  

An instance method is invoked on objects. A class method is invoked on class.

在对象上调用实例方法。在类上调用类方法。

For example the line:

例如,行:

SomeClass *object = [[SomeClass alloc] init];

SomeClass * object = [[SomeClass alloc] init];

Here you can see that the "alloc" works on "SomeClass" and not on "object".

在这里你可以看到“alloc”适用于“SomeClass”而不适用于“object”。

Whereas: [object callMyFunction]; will act on "object" and not "class". This is an instance method.

鉴于:[object callMyFunction];将作用于“对象”而不是“类”。这是一个实例方法。

#3


0  

The main difference with those two is the former one ie with single dash before it is only called by the instance of that class where it is declared ie one have to create one instance of that class means one object for that class and using . one can call the instance method

与这两者的主要区别在于前一个,即在单个破折号之前,它只被声明它的类的实例调用,即必须创建该类的一个实例意味着该类的一个对象并使用。一个人可以调用实例方法

In class method, the later one can be called directly using the class name. To call class methods one dosen't need any object.

在类方法中,可以使用类名直接调用后一个方法。要调用类方法,不需要任何对象。

Please refer this link from apple developers documents

请参阅Apple开发者文档中的此链接

#1


2  

The difference between a class method and an instance method is that an instance method requires an instance of the class on which it will (generally) operate. The message to invoke an instance method must be sent to an instance of a class.

类方法和实例方法之间的区别在于实例方法需要它将(通常)操作的类的实例。必须将调用实例方法的消息发送到类的实例。

Probably the most common single use of class methods is object factories; messages that you send to a class to create an instance configured according to the parameters you've sent in. For example in Cocoa the NSString class has several class methods named stringWithSomethingOrOther: that will create a new NSString object and hand it back to you.

可能最常见的一类方法是对象工厂;您发送给类的消息,以创建根据您发送的参数配置的实例。例如,在Cocoa中,NSString类有几个名为stringWithSomethingOrOther的类方法:它将创建一个新的NSString对象并将其交还给您。

On the other hand, NSString also has many instance methods - operations which really have no meaning without an actual instance to work with. A commonly-used one might be the length method, which tells you how many characters are in the specific NSString instance to which the message is sent.

另一方面,NSString也有许多实例方法 - 在没有实际实例的情况下实际没有意义的操作。常用的方法可能是length方法,它告诉您发送消息的特定NSString实例中有多少个字符。

Also see this. What is the difference between class and instance methods?

也看到这个。类和实例方法有什么区别?

#2


0  

An instance method is invoked on objects. A class method is invoked on class.

在对象上调用实例方法。在类上调用类方法。

For example the line:

例如,行:

SomeClass *object = [[SomeClass alloc] init];

SomeClass * object = [[SomeClass alloc] init];

Here you can see that the "alloc" works on "SomeClass" and not on "object".

在这里你可以看到“alloc”适用于“SomeClass”而不适用于“object”。

Whereas: [object callMyFunction]; will act on "object" and not "class". This is an instance method.

鉴于:[object callMyFunction];将作用于“对象”而不是“类”。这是一个实例方法。

#3


0  

The main difference with those two is the former one ie with single dash before it is only called by the instance of that class where it is declared ie one have to create one instance of that class means one object for that class and using . one can call the instance method

与这两者的主要区别在于前一个,即在单个破折号之前,它只被声明它的类的实例调用,即必须创建该类的一个实例意味着该类的一个对象并使用。一个人可以调用实例方法

In class method, the later one can be called directly using the class name. To call class methods one dosen't need any object.

在类方法中,可以使用类名直接调用后一个方法。要调用类方法,不需要任何对象。

Please refer this link from apple developers documents

请参阅Apple开发者文档中的此链接