开发者

Can I change the super of a class?

开发者 https://www.devze.com 2023-01-08 04:33 出处:网络
Is it somehow possible to choose the sup开发者_运维技巧er of a class (preferably in the alloc or init method) so my class inherits from something else?You can do that in -init by creating an instance

Is it somehow possible to choose the sup开发者_运维技巧er of a class (preferably in the alloc or init method) so my class inherits from something else?


You can do that in -init by creating an instance of the desired target class and resetting self. Be sure to send a -release message to the previous instance if you do that, though.


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.)

0

精彩评论

暂无评论...
验证码 换一张
取 消