ObjC: must I specify the inheritance in the header file?
Common examples for a ObjC object are like this (for the header file):
@interface A: B {
int x;
int y;
}
@end
Is it possible to avoid the inheritance specification (i.e. B
here) in the header file?
In my case, the framework A defines that class A
and another (sub-)framework B defines the class B
(which is a subclass of NSView
). A links to B. In my application, I link to A and I don't need to know anything about B exce开发者_如何学Pythonpt that it is a subclass of NSView
. And I want to avoid to link to B. But if B
is in the header file, I think I cannot avoid it, that's why I was asking about how to avoid it.
No. You have to specify the superclass for any subclass. May I ask why you would want to do something like this?
Your application will need the code for B, therefore you must either link to B's framework, or compile the B framework into your A framework. Either way, you cannot use an instance of A without the code for B, and you must include B's header in your A header.
no.
you must often work around this with a class cluster, hold a private implementation, or create an object factory. then you can minimize the dependencies across modules.
you'll still ultimately need to link to the sub library at some stage if you intend to use it (e.g. create an instance of).
Update - Demonstrate Private Implementations
Private implementations can be entirely opaque. If you do expose them, here are two ways to implement private implementations which are visible to clients:
via protocol:
// MONDrawProtocol.h
// zero linkage required
// needs to be visible to adopt
// may be forwarded
@protocol MONDrawProtocol
- (void)drawView:(NSView *)view inRect:(NSRect)rect;
@end
// MONView.h
@protocol MONDrawProtocol;
@interface MONView : NSView
{
NSObject<MONDrawProtocol>* drawer;
}
@end
// MONView.m
#include "MONDrawProtocol.h"
@implementation MONView
- (void)drawRect:(NSRect)rect
{
[self.drawer drawView:self inRect:rect];
}
@end
via base:
// MONDrawer.h
// base needs to be visible to subclass and types which use MONDrawer
// may be forwarded
@interface MONDrawer : NSObject
- (void)drawView:(NSView *)view inRect:(NSRect)rect;
@end
// MONView.h
@class MONDrawer;
@interface MONView : NSView
{
MONDrawer * drawer;
}
@end
// MONView.m
#include "MONDrawer.h"
@implementation MONView
- (void)drawRect:(NSRect)rect
{
[self.drawer drawView:self inRect:rect];
}
@end
If you don't specify a superclass in the interface, then your class is a root class. This means it doesn't inherit from any other class, so it is responsible for providing its own implementation of the required methods (most of those defined by the NSObject class and protocol). Since this is not a simple task, it is highly encouraged that you inherit from some other class which provides these methods.
Yes you can, with that you will also lost default implementations of alloc, init, etc. Which makes you write your own alloc, init and other stuffs which was there in NSObject
can't you just include a mock version of the class you're inheriting from A's header itself? Not sure if that will cause problems, but it would allow you to clean up your linking requrirements a bit. B-new could then be a category of B'Original
I have one solution now. Instead of providing the class, I just provide a function like this:
NSView* allocA();
Internally in the framework A, A
is a subclass of B
.
精彩评论