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

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

问题描述

我使用过几种不同的语言,例如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?

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

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.

这恰好描述了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,这正是为什么许多类可以在NSObject上共享+alloc的单个,简单实现的原因)需要它们自己的自定义实现),并且调用类方法的过程与其他任何方法调用站点都通过完全相同的基于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天全站免登陆