What's the difference between “class method” a

2019-01-03 02:41发布

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?

4条回答
家丑人穷心不美
2楼-- · 2019-01-03 02:55

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

查看更多
何必那么认真
3楼-- · 2019-01-03 03:00

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 ...
}    
查看更多
干净又极端
4楼-- · 2019-01-03 03:04

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.

查看更多
唯我独甜
5楼-- · 2019-01-03 03:09

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.

查看更多
登录 后发表回答