rendered paste body//
// Entity.h
// inheritance
//
#import <Foundation/Foundation.h>
@interface Entity : NSObject {
}
- (void)sayPleaseWork;
@end
//
// Entity.m
// inheritance
#import "Entity.h"
@implementation Entity
- (void)sayPleaseWork
{
NSLog(@"Please work.");
}
@end
//
// Action.h
// inheritance
#import "Entity.h"
@interface Action : NSObject {
Entity *__anEntity;
}
- (Entity *)anEntity;
- (void)setAnEntity:(Entity *)newAnEntity;
@end
//
// Action.m
// inheritance
#import "Action.h"
@implementation Action
// init
- (id)init
{
if (self = [super init]) {
// [self setAnEntity: <(Entity *)newAnEntity>];
}
return self;
}
//===========================================================
// anEntity
//===========================================================
- (Entity *)anEntity
{
return [[__anEntity retain] autorelease];
}
- (void)setAnEntity:(Entity *)newAnEntity
{
//NSLog(@"in -setAnEntity:, old value of __anEntity: %@, changed to: %@", __anEntity, newAnEntity);
if (__anEntity != newAnEntity) {
[__anEntity release];
__anEntity = [newAnEntity retain];
}
}
//===========================================================
// dealloc
//===========================================================
- (void)dealloc
{
[self setAnEntity:nil];
[super dealloc];
}
@end
//
// SomeAction.h
// inheritance
#import "Action.h"
@interface SomeAction : Action {
}
- (void)doThing;
@end
//
// SomeAction.m
// inheritance
#import "SomeAction.h"
@implementation SomeAction
- (void)doThing
{
NSLog(@"SomeAction -doThing");
}
@end
#import "Entity.h"
#import "SomeAction.h"
int main (int argc, const char * argv[]) {
Entity *dude = [Entity new];
SomeAction *foo = [SomeAction new];
// *foo is an instance of @SomeAction.
// @SomeAction is a subclass of @Action
// @Action has ivar __anEntity
// __anEntity is of type @Entity
// @Entity has a method -sayPleaseWork
[dude sayPleaseWork]; // works, sayPleaseWork is in @Entity
[foo doThing]; // works, doThing is in @Action.h
// how can we call [*bar -sayPleaseWork]?
Entity *bar = [foo anEntity];
[bar sayPleaseWork]; // don't works, but compiles.
// at this point we should have logged nslog 3 times
// "please work"
// "doThing"
// "please work" --- this doesn't print :-(
[bar release];
[foo release];
[dude release];
return 0;
}