什么时候 NSObject 不被声明为父类?
例如:
@interface Fraction: NSObject {
...
什么时候不会使用 NSObject
以及 NSObject
是所有其他类的最终父类吗?
请随时纠正我使用的任何错误术语。
For example:
@interface Fraction: NSObject {
...
When wouldn't NSObject
be used and is NSObject
the ultimate parent class for all other classes?
Please feel free to correct me on any wrong terminology used.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在任何 Cocoa 应用程序中,如果您检查运行时类树,您将发现五个根类:NSObject、Object、NSProxy、NSMessageBuilder 和 NSZombie。
应用程序中的大多数普通对象都是 NSObject 的后代。 Object 是 Objective-C 早期的原始根类,在 NeXT 开发企业对象框架时被 NSObject 取代。
NSMessageBuilder 被运行时用来进行消息转发。 分布式对象实现使用 NSProxy 来处理编组消息,以发送到其他进程甚至其他主机上的对象。 NSZombie 是一个用于调试内存泄漏的类。
In any Cocoa app, if you examine the runtime class tree, you will find five root classes: NSObject, Object, NSProxy, NSMessageBuilder, and NSZombie.
Most ordinary objects in your app will be descended from NSObject. Object is the original root class from the early days of Objective-C, and it was superseded by NSObject when NeXT was developing the Enterprise Objects Framework.
NSMessageBuilder is used by the runtime for message forwarding. NSProxy is used by the distributed objects implementation to deal with marshaling messages to send to objects in other processes or even on other hosts. NSZombie is a class that's used for debugging memory leaks.
Cocoa 有两个根类:NSObject 和 NSProxy。
NSObject 是几乎所有 Cocoa 对象的根类。
NSProxy 是一个抽象超类,为充当其他对象或尚不存在的对象的替身的对象定义 API,并且是 NSDistantObject 等类的根类。
这两个类都实现了 NSObject 协议。
也可以编写自己的没有根类的 Objective C 类,但您可能永远不会这样做,并且除非您还实现了 NSObject 协议,否则您将无法将它与 Cocoa 一起使用来完成很多事情,即便如此,它的用途也是值得怀疑的。
Cocoa has two root classes: NSObject and NSProxy.
NSObject is the root class of almost all Cocoa objects.
NSProxy is an abstract superclass defining an API for objects that act as stand-ins for other objects or for objects that don’t exist yet, and is the root class for classes like NSDistantObject.
Both classes implement the NSObject protocol.
It is also possible to write your own Objective C classes that do not have a root class, but you would probably never do that, and you would not be able to use it with Cocoa for much of anything unless you also implemented the NSObject protocol, and even then it would be of dubious use.
如果我正在编写 NSView 的子类,我会写:
因为我的类继承自 NSView。 Objective-C 中的类仅继承自单个父类。 当您从 NSView 沿着链向上(到其父类 NSResponder)时,您会发现它最终继承自 NSObject。
我不确定你问题的第二部分。 我认为 Apple 的 Cocoa 框架中的所有类最终都继承自 NSObject,但不要引用我的观点。 在 Objective-C 中,没有理由不能有其他根对象(否则 Objective-C 只能在 Apple 系统上使用)。 然而,在 Cocoa 中,NSObject 是根。
If I was writing a subclass of NSView, I would write:
because my class is inheriting from NSView. Classes in Objective-C inherit from only a single parent class. As you work your way up the chain from NSView (to its parent class, NSResponder), you would find that it eventually inherits from NSObject.
I'm not sure about the second part of your question. I think all classes in Apple's Cocoa Frameworks eventually inherit from NSObject, but don't quote me on that. There's no reason, in Objective-C, why there couldn't be other root objects (otherwise Objective-C would only be used on Apple systems). However, in Cocoa, NSObject is the root.