Objective-C 类和该类实例之间的区别
有人可以向我解释一下类和类的实例之间有什么区别吗? 如果我在程序中只能使用某个类的一个实例,我可以像使用实例一样使用该类,并将方法声明中的所有 (-) 更改为 (+) 吗?类方法和实例方法有什么区别。谢谢
Could someone explain to me what is the difference between class and instance of the class.
If I can use only one instance of the some class in the program, can I use the class like an instance and change all the (-) with (+) in the methods declaration. What is the difference between the class and instance methods. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这似乎是几个问题:
首先,类和类的实例之间的区别在于,类是实例的规范。该类将始终存在,但您必须创建一个实例才能使用该类的实例方法。类创建实例并为它们提供方法。
其次,“如果我在程序中只能使用一个类的一个实例”是一种永远不会出现的情况。您可以根据需要创建任意多个类的实例(硬件允许)。
第三,类和实例方法之间的区别在于,虽然必须创建实例才能使用实例方法,但类方法只是类提供的有用函数,而无需从该类创建对象。实例方法对特定实例的属性/字段进行操作,而类方法仅接受输入并返回独立于任何实例的值。
This seems to be several questions:
First, the difference between a class and an instance of a class is that a class is a specification for an instance. The class will always be there, but you must create an instance to use the instance methods of that class. The class is what creates instances and gives them methods.
Second, "if I can use only one instance of a class in a program" is a situation that will never come up. You can make as many instances of a class as you want (hardware permitting).
Third, the difference between a class and an instance method is that while you must create an instance to use an instance method, class methods are just useful functions that the class offers without creating an object from that class. Instance methods operate on the properties/fields of specific instances, while class methods merely accept input and return values independent of any instances.
请访问 Cocoa 设计模式 用于类的单个实例的单例。
Please visit Cocoa Design Pattern Singleton for single instance of class.