简体   繁体   English

在Objective-C中使用类方法、实例方法的地方

[英]Where to Use Class Method ,Instance Method in Objective-C

"Instance" mean in Objective-C? “实例”在Objective-C 中是什么意思? Kindly tell me where to use Class Method And where to use Instance Method,also tell me where we use (Instacetype) method?请告诉我在哪里使用类方法以及在哪里使用实例方法,还告诉我我们在哪里使用(Instacetype)方法? why/where we use multi Parameters?为什么/我们在哪里使用多参数?

A class method is a method whose self parameter is a reference to the class's class object.类方法是一种方法,其 self 参数是对类的类对象的引用。

An instance method is a method whose self parameter is a reference to a specific instance of the class.实例方法是一种方法,其 self 参数是对类的特定实例的引用。

Those are the technical differences.这些是技术差异。

A more practical answer is that an instance method operates on a single instance of the class, while a class method operates at a more global, non-specific level.更实际的答案是,实例方法在类的单个实例上运行,而类方法在更全局、非特定的级别上运行。 A class method can act as a factory method, such as NSString 's stringWithFormat: method.类方法可以充当工厂方法,例如NSStringstringWithFormat:方法。 It can also be used to configure behavior that will affect all instances of the class.它还可用于配置将影响类的所有实例的行为。 It can also be used to operate on a collection of instances of the class, such as sorting or filtering.它还可以用于对类的实例集合进行操作,例如排序或过滤。

instancetype is a keyword that can be used as a placeholder for the current class's type. instancetype是一个关键字,可用作当前类类型的占位符。 It says to the compiler: pretend that I wrote <my class name> here, so if you see the result of this method assigned somewhere, you know what type it's supposed to be.它对编译器说:假设我在这里写了 <my class name>,所以如果你看到这个方法的结果被分配到某个地方,你就知道它应该是什么类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM