“类方法"和“类方法"有什么区别?和“静态方法"? [英] What's the difference between "class method" and "static method"?

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

问题描述

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

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

在大多数语言中,不需要对象实例的方法称为静态方法.然而,当谈到 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?

推荐答案

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

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?

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

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

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

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

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.

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

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 的类方法可以在整个类层次结构中被覆盖,并且它们可以被混合.在通常提供静态方法代替类方法的语言中,这些都不支持.

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 ...
}    

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

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