我可以更改班级的 super 吗?
是否可以以某种方式选择一个类的超级(最好是在 alloc 或 init 方法中),以便我的类继承自其他类?
Is it somehow possible to choose the super of a class (preferably in the alloc or init method) so my class inherits from something else?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在
-init
中通过创建所需目标类的实例并重置self
来完成此操作。不过,如果您这样做,请务必向前一个实例发送-release
消息。You can do that in
-init
by creating an instance of the desired target class and resettingself
. Be sure to send a-release
message to the previous instance if you do that, though.不,你不能。 并且:是的,可以。
类(!)是一个相对静态的东西。它与其超类是“硬连线”的。这很重要,因为必须有一些东西,知道如何处理分配的内存和其他东西。
不同的是一个实例。您可以通过“询问”某个类的类来获取某个类的实例。 (而且,通常,在获得一个之后,您会要求初始化等等。)
此时,通常的行为可能会被“破坏”。但是,我认为,这对初学者来说没什么用,做这样的事情你应该有经验!
为什么?阅读苹果公司对此的说法。你看,连苹果公司也做这样的事!看看像 NSNumber 这样的类簇: Cocoa 基础指南(不能发布比这更多的内容,但无需我的帮助,您肯定会找到 NSNumber 文档。)
No, you can’t. And: yes, you can.
A class(!) is a relative static thing. It is “hard wired” to its superclass. This is important as there must be some thing, knowing how to handle the allocated memory and other things.
Something different is an instance. You get an instance of some class by “asking” its class for one. (And, usually, after getting one you ask for initialization and so on.)
At this point the usual behaviour can be “broken”. But, as I think, this is nothing to do for beginners, you should be experienced when doing such tings!
Why? Read the stuff, Apple is saying about this. And see, that even Apple does such things! Have a look at class clusters like NSNumber: Cocoa Fundamentals Guide (Can’t post more than this one, but you surely find NSNumber-documentation without my help.)