“类方法”和“类方法”有什么区别?和“静态方法”?

发布于 2024-12-15 04:47:57 字数 176 浏览 4 评论 0原文

我使用过几种不同的语言,例如 Java、C# 和 Objective-C。

在大多数语言中,不需要对象实例的方法称为静态方法。然而,当谈到 Objective-C 时,有些人在你称它们为静态方法时会产生抵触情绪,他们希望你称它们为类方法。

为什么它们被称为类方法而不是静态方法?静态方法和类方法有什么区别?

I've worked with a few different languages such as Java, C#, and Objective-C.

In most languages, methods that don't require an instance of an object are called static methods. However, when it comes to Objective-C, some people get defensive when you call them static methods, and they expect you to call them class methods.

Why are they called class methods instead of static methods? What is the difference between a static method and a class method?

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

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

发布评论

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

评论(5

灰色世界里的红玫瑰 2024-12-22 04:47:58

因为它是动态绑定的,而不是静态的。

因为它实际上是一个类对象的实例方法。

Objective-C 类方法实际上是对象的类对象的实例方法。

很难用文字描述。请参阅此处的精美插图。

http://www.sealiesoftware.com/blog/archive/2009 /04/14/objc_explain_Classes_and_metaclasses.html

Because it's dynamically bound, not static.

Because it's really a class object's instance method.

Objective-C class method is actually an object's class object's instance method.

It's hard to describe with text. See nice illustration here.

http://www.sealiesoftware.com/blog/archive/2009/04/14/objc_explain_Classes_and_metaclasses.html

眼波传意 2024-12-22 04:47:58

尽管类方法和静态方法在实践中大多数时候是相同的,但它们是不同的。对于静态方法,类充当命名空间限定符。对于类方法,类本身就是一个对象,因此类方法对于类对象来说与实例方法对于实例来说是完全相同的;因此,您可以执行以下操作,

@interface TestClass : NSObject
+ (void)classOrInstanceMethod;
- (void)classOrInstanceMethod;
@end
...
NSArray * arr = [NSArray arrayWithObjects:
                        [[[TestClass alloc] init] autorelease],
                        [TestClass class],
                        nil];
for( id obj in arr )
    [obj classOrInstanceMethod];

调用哪个版本的 classOrInstanceMethod 取决于 obj 是类对象还是实例。如果您熟悉工厂类模式,那么该模式是 Objective-C 语言的一部分。

Though class methods and static methods are in practice the same most of the time, they are different. With static methods the class is acting as a namespace qualifier. With class methods the class itself is an object and so class methods are to the class object exactly the same thing instance methods are to an instance; as a consequence you can do the following

@interface TestClass : NSObject
+ (void)classOrInstanceMethod;
- (void)classOrInstanceMethod;
@end
...
NSArray * arr = [NSArray arrayWithObjects:
                        [[[TestClass alloc] init] autorelease],
                        [TestClass class],
                        nil];
for( id obj in arr )
    [obj classOrInstanceMethod];

which version of classOrInstanceMethod is called depends on whether obj is a class object or and instance. If you are familiar with the factory class pattern, this pattern is part of the Objective-C language.

混吃等死 2024-12-22 04:47:58

这纯粹是一个历史差异,主要是因为 Objective-C 是与 C++ 同时开发的,并且在 C++ 之前或后来的 Java 和 C# 等语言产生了很大的影响。 Objective-C 本质上是 Smalltalk 对象模型到 C 的端口,因此它的语法和术语不一定看起来像 C++ 使用的那样“类似 C”。然而,Objective-C 不使用术语“静态方法”绝不是逆潮流,因为这种趋势早在 1983 年就尚未确立。

This is purely a historical difference, mostly stemming from the fact that Objective-C was developed contemporaneously with C++, and before C++ or later languages like Java and C# had much influence. Objective-C was essentially a port of the Smalltalk object model to C, so its syntax and terminology don't necessarily seem as "C-like" as that used by C++. However, Objective-C was in no way bucking a trend by not using the term "static method", because that trend wasn't well-established back in 1983.

≈。彩虹 2024-12-22 04:47:58

上课方法:
OOP 中的类方法是绑定到类而不是类实例的方法。它们是在类定义中定义的,并使用类本身而不是类的实例来访问。 @classmethod 装饰器用于定义类方法。

静态方法:
在 OOP 中,静态方法是属于类而不是类实例的方法。它与类本身相关联,而不是与从类创建的特定对象相关联。静态方法可以直接在类本身上调用,无需创建实例。这里使用了@staticmethod装饰器。

另外,还有另一种方法,称为实例方法。您可以在此处阅读差异

Class Method:
Class methods in OOP are methods that are bound to a class rather than an instance of the class. They are defined within the class definition and are accessed using the class itself, rather than an instance of the class. @classmethod decorator is used to define a class method.

Whereas

Static Method:
In OOPs, a static method is a method that belongs to a class rather than an instance of the class. It is associated with the class itself, rather than with specific objects created from the class. Static methods can be called directly on the class itself without the need to create an instance. Here, @staticmethod decorator is used.

Also, there is another method called instance method. You can read the differences here.

勿忘初心 2024-12-22 04:47:57

所以我的问题是为什么它们被称为类方法而不是
静态方法?静态方法和静态方法有什么区别
类方法?

来自维基百科:静态方法既不需要类的实例,也不能隐式访问此类实例的数据(或 this、self、Me 等)。

这准确地描述了 Objective-C 的类方法不是

Objective-C 类方法非常需要一个作为方法调用目标的实例。也就是说,它需要一个描述被调用的类对象的元类实例。

与静态方法不同,Objective-C 的类方法可以被继承(这与前面提到的 self 相结合,正是许多类可以共享一个简单的 +alloc< 实现的原因) /code> 在 NSObject 上,不需要自己的自定义实现)并调用类方法会经历与任何其他方法调用站点完全相同的基于 objc_msgSend* 的调度机制。

Objective-C 的类方法可以在类层次结构中重写,并且可以进行混合。通常提供静态方法代替类方法的语言都不支持这些。

最重要的是,静态方法和类方法非常不同。虽然这种差异对于日常编码目的来说基本上是透明的,但在某些情况下,了解类方法的工作原理可以为您节省大量不必要的代码行。

例如,您不能使用静态方法执行此操作:

@interface AbstractClass:NSObject
+ factory;
@end

@implementation AbstractClass
+ factory
{
    return [[[self alloc] init] autorelease];
}
@end

@interface Concrete1:AbstractClass
@end
@implementation Concrete1
@end
@interface Concrete2:AbstractClass
@end
@implementation Concrete2
@end

void foo() {
    Concrete1 *c = [Concrete1 factory];
    Concrete2 *d = [Concrete2 factory];
    ... etc ...
}    

So my question is why are they called class methods instead of a
static method? What is the difference between a static method and a
class method?

From Wikipedia: Static methods neither require an instance of the class nor can they implicitly access the data (or this, self, Me, etc.) of such an instance.

This describes exactly what Objective-C's class methods are not.

An Objective-C class method very much requires an instance that is the target of the method invocation. That is, it requires an instance of the metaclass that describes the class object being invoked.

Unlike static methods, Objective-C's class methods can be inherited (which, in combination with having the aforementioned self, is exactly why many classes can share a single, simple, implementation of +alloc on NSObject without needing their own custom implementations) and invoking a class method goes through the exact same objc_msgSend* based dispatch mechanism as any other method call site.

Objective-C's class methods can be overridden across the class hierarchy and they can be swizzled. None of which is supported in languages that typically offer static methods in lieu of class methods.

The bottom line is that static methods and class methods are very different. While that difference is mostly transparent for day to day coding purposes, there are still situations where knowing how class methods work can save you a ton of unnecessary lines of code.

For example, you can't do this with static methods:

@interface AbstractClass:NSObject
+ factory;
@end

@implementation AbstractClass
+ factory
{
    return [[[self alloc] init] autorelease];
}
@end

@interface Concrete1:AbstractClass
@end
@implementation Concrete1
@end
@interface Concrete2:AbstractClass
@end
@implementation Concrete2
@end

void foo() {
    Concrete1 *c = [Concrete1 factory];
    Concrete2 *d = [Concrete2 factory];
    ... etc ...
}    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文