Objective-C中实例方法和类方法的目的

时间:2022-09-07 07:25:01

I have checked out all these questions...

我检查了所有这些问题……

...and all they explain is how instance methods are used on instances of a class and class methods are used with the class, when a message is sent to a class. This is helpful, but I'm curious to know why one would use a class method vs. an instance method.

…它们所解释的是,当消息被发送到类时,类的实例方法是如何在类的实例中使用的,类方法是如何与类一起使用的。这很有帮助,但是我很想知道为什么人们会使用类方法而不是实例方法。

1 个解决方案

#1


45  

Generally speaking, you should create instance methods when you need code that operates on a specific instance of an object. You create a class method when you need to do something that involves that class in general but probably doesn't operate on any specific objects of that class.

一般来说,当需要对对象的特定实例进行操作的代码时,应该创建实例方法。当您需要做一些通常涉及该类但可能不会对该类的任何特定对象进行操作的事情时,您可以创建一个类方法。

In practice, you will find that nearly all of your methods should be instance methods. Just take a look at any existing Objective-C class like NSString, NSArray, UIView, etc. and you'll see that the vast majority of their methods are instance methods. The most common use of class methods (again, look at the classes I mentioned) are for convenience constructors that return autorelease objects, or singleton accessors.

在实践中,您将发现几乎所有的方法都应该是实例方法。看看任何现有的Objective-C类,比如NSString, NSArray, UIView,等等,你会发现它们的绝大多数方法都是实例方法。类方法最常用的用法(再看一下我提到的类)是为了方便构造函数,它返回自动发出的对象或单例访问器。

Consider the length method in NSString. Why is this an instance method and not a class method? It is an instance method because it only makes sense to ask a specific instance of NSString what its length is. Asking NSString in general for a length (i.e. if length was a class method) wouldn't make any sense.

考虑NSString中的length方法。为什么这是一个实例方法而不是一个类方法?它是一个实例方法,因为询问一个特定的NSString实例的长度是有意义的。一般地问NSString一个长度(例如,如果length是一个类方法)是没有意义的。

On the other hand, let's say that we want to add a method to NSNumber that will return the maximum integer value that can be stored on a given system. In this case, it should be a class method because we're just asking a general question of NSNumber that is independent of any specific instance.

另一方面,假设我们要向NSNumber添加一个方法,该方法将返回可以存储在给定系统上的最大整数值。在这种情况下,它应该是一个类方法因为我们只是问一个关于NSNumber的一般性问题它独立于任何特定的实例。

#1


45  

Generally speaking, you should create instance methods when you need code that operates on a specific instance of an object. You create a class method when you need to do something that involves that class in general but probably doesn't operate on any specific objects of that class.

一般来说,当需要对对象的特定实例进行操作的代码时,应该创建实例方法。当您需要做一些通常涉及该类但可能不会对该类的任何特定对象进行操作的事情时,您可以创建一个类方法。

In practice, you will find that nearly all of your methods should be instance methods. Just take a look at any existing Objective-C class like NSString, NSArray, UIView, etc. and you'll see that the vast majority of their methods are instance methods. The most common use of class methods (again, look at the classes I mentioned) are for convenience constructors that return autorelease objects, or singleton accessors.

在实践中,您将发现几乎所有的方法都应该是实例方法。看看任何现有的Objective-C类,比如NSString, NSArray, UIView,等等,你会发现它们的绝大多数方法都是实例方法。类方法最常用的用法(再看一下我提到的类)是为了方便构造函数,它返回自动发出的对象或单例访问器。

Consider the length method in NSString. Why is this an instance method and not a class method? It is an instance method because it only makes sense to ask a specific instance of NSString what its length is. Asking NSString in general for a length (i.e. if length was a class method) wouldn't make any sense.

考虑NSString中的length方法。为什么这是一个实例方法而不是一个类方法?它是一个实例方法,因为询问一个特定的NSString实例的长度是有意义的。一般地问NSString一个长度(例如,如果length是一个类方法)是没有意义的。

On the other hand, let's say that we want to add a method to NSNumber that will return the maximum integer value that can be stored on a given system. In this case, it should be a class method because we're just asking a general question of NSNumber that is independent of any specific instance.

另一方面,假设我们要向NSNumber添加一个方法,该方法将返回可以存储在给定系统上的最大整数值。在这种情况下,它应该是一个类方法因为我们只是问一个关于NSNumber的一般性问题它独立于任何特定的实例。