Java 如何在 Objective-C 中创建静态方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2691383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I make a static method in Objective-C?
提问by Tattat
In Java, I may have a class, for example, Utility
and I have a static method called changeToCapitalLetter
, so I can do something like this:
例如,在 Java 中,我可能有一个类,Utility
并且我有一个名为 的静态方法changeToCapitalLetter
,因此我可以执行以下操作:
Utility.changeToCapitalLetter(myString);
How can I do the similar thing in Objective C?
我怎样才能在 Objective C 中做类似的事情?
Thanks a lot
非常感谢
采纳答案by gammelgul
回答by Shekhu
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、self、Me 等)。
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 类方法非常需要一个作为方法调用目标的实例。也就是说,它需要一个描述被调用类对象的元类实例。
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 的类方法可以被继承(结合前面提到的 self,这正是为什么许多类可以在 NSObject 上共享一个简单的 +alloc 实现而不需要它们自己的自定义实现)并调用类方法通过与任何其他方法调用站点完全相同的基于 objc_msgSend* 的调度机制。
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.
Objective-C 的类方法可以在整个类层次结构中被覆盖,并且它们可以被混合。在通常提供静态方法代替类方法的语言中,这些都不支持。
Though class methods and static method are in practice the same most of the time, they are different. With static methods the 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
@implementation TestClass
+(void)classOrInstanceMethod{
NSLog(@"%s", __PRETTY_FUNCTION__);
}
-(void)classOrInstanceMethod{
NSLog(@"%s", __PRETTY_FUNCTION__);
}
@end
int main(int argc, const char* argv[])
{
@autoreleasepool{
NSLog(@"Static method in ObjectiveC");
}
NSArray* arr = [NSArray arrayWithObjects:[[TestClass alloc]init],
[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.
调用哪个版本的 classOrInstanceMethod 取决于 obj 是类对象还是实例。如果您熟悉工厂类模式,则该模式是 Objective-C 语言的一部分。
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.
归根结底,静态方法和类方法非常不同。虽然这种差异对于日常编码目的来说几乎是透明的,但在某些情况下,了解类方法的工作方式可以为您节省大量不必要的代码行。