类方法和实例方法 - 何时/何时不使用它们?

发布于 2024-11-17 03:19:59 字数 76 浏览 9 评论 0 原文

我想知道何时以及何时不使用类方法和实例方法。我需要一些实际的例子。我真的很困惑。另一个问题:我们不能用实例方法做与类方法完全相同的事情吗?

I was wondering when and when not to use class methods and instance methods. I need some practical examples. I am really confused. Another question: can't we do exactly the same things with instance methods that we can with class methods?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

静待花开 2024-11-24 03:19:59

类方法:该方法不绑定到任何特定对象。在某种程度上,它的作用类似于类名称空间中的自由函数。没有“自我”指针。例如 [UIScreen mainScreen] 是一种类方法,因为只有一个屏幕,无需关心多个“屏幕实例”。

实例方法:绑定到特定对象。

这适用于大多数 OO 语言,而不仅仅是 obj-C。

Class methods: The method isn't tied to any specific object. In a way it acts like a free function in the class's namespace. No 'self' pointer. For instance [UIScreen mainScreen] is a class method because there's only one screen and there's no need to care about multiple 'screen instances'.

Instance method: Tied to a specific object.

This applies to most OO languages, not just obj-C.

岁月如刀 2024-11-24 03:19:59

在实现级别,实例方法调用包含指向数据结构(对象)的隐藏指针,而类方法则不然。

要问的实际问题是您的调用是否需要向调用发送一些特定数据,这些数据是否或最好封装为对象内的实例数据。

您(通常)可以使用类方法作为实例方法执行相同的操作,但是您必须在调用中显式地将对象作为可见参数传递,这看起来更难看,并且还可能禁用 Objective C 语言的某些方法重写功能。

At the implementation level, an instance method call contains a hidden pointer to a data structure (the object), a class method does not.

The practical question to ask is whether your call requires sending the call some specific data which is or could best be encapsulated as instance data inside an object, or not.

You (usually) can do the same thing with class methods as instance methods, but then you have to explicitly pass the object as a visible parameter in the call, which is uglier looking and also potentially disables some method override features of the Objective C language.

琴流音 2024-11-24 03:19:59

对实用函数使用类方法,对面向对象的东西使用实例方法。
例如。对于数学计算(例如 sin x ),请使用类方法。但是为了调用特定于对象的行为..使用实例方法..

Use class methods for utility functions and Instance methods for object oriented stuff.
Eg. For Mathematical calculation (eg sin x ) use class method. But for invoking a behavior specific to an object.. use instance method ..

白首有我共你 2024-11-24 03:19:59

顾名思义,类方法与类绑定。您可以仅使用特定类的名称来调用它们。这些通常可以是类的公开方法。
例如
NSArray

+ (id)arrayWithArray:(NSArray *)array;

您可以使用类名 NSArray 来调用它。您所期望的只是创建该特定类类型的对象。这不需要调用对象。此外,这些都是非常基本的方法,因此最好将其作为类方法。

另一方面,顾名思义,实例方法与实例密切相关。对象是封装类的状态(ivars)和行为(方法)的实体。这对于对象来说可能是非常具体的。
例如

- (NSUInteger)count;

让我们采用 NSArray *aNSArray *b。如果 a 包含 5 个项目,而 b 包含 4 个项目,则调用这些实例的实例方法将产生不同的结果。这就是为什么我们需要在调用实例方法时初始化实例。它们在被调用的对象的上下文(或状态)上工作。它们也不像类方法那样公开。

希望这有帮助。

A class method as the name implies is bounded to the class. You can invoke them just with the name of the particular class. These can be normally exposed methods of a class.
For example
NSArray

+ (id)arrayWithArray:(NSArray *)array;.

You call it with the class name NSArray. What you expect is just a creation of a object of the type of that particular class. This doesn't need an object to invoke. Also these are very basic method required so its better to make it as a class method.

On the other hand instance method as the name implies is very much bound to the instance. Object is an entity that encapsulates state (ivars) and behaviors (methods) of a class. This can be very specific to the object.
For example

- (NSUInteger)count;

Lets take NSArray *a and NSArray *b. If a contains 5 items whereas b contains 4, instance methods called upon these instances will produce different results. And thats why we need instances to be initialized while invoking instance method. They work on the context(or state) of the object they are been called upon. Also they are not exposed as the class methods are.

Hope this helps.

莫言歌 2024-11-24 03:19:59

如果您想使用实例对象或实例变量,则必须使用实例方法。
Bcz 在类内部,您无法访问 Instance 实例对象或实例变量。

类方法是静态方法。

If you want to use instance objet or instance variable you have to go with instance Methods.
Bcz Inside the class you cant access the Instance instance objet or instance variable.

Class methods are static methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文