Accessing objects within objects?
I am a little confused about how I "set" and "get" the instance variables for an object that is contained within another. As you can see from the code below I first create a RocketBody which includes a SolidRocketMotor object. I am creating the SolidRocketMotor in RocketBody init and deallocating it in RocketBody dealloc.
My question is how would I go about setting / getting the engine (SolidRocketMotor) instance variables from within main (e.g. setting engCase)?
int main (int argc, const char * argv[]) {
RocketBody *ares_1x;
ares_1x = [[RocketBody alloc] init];
[ares_1x setFuel: [NSNumber numberWithInt:50000]];
[ares_1x setWeight:[NSNumber numberWithFloat:1.8]];
// How to set engCase to "Standard" here?
...
.
@interface SolidRocketMotor : NSObject
{
NSString *engCase;
NSString *engPropellant;
NSNumber *engIgniter;
NSString *engNozzle;
}
@property(copy) NSString *engCase;
@property(copy) NSString *engPropellant;
@property(copy) NSNumber *engIgniter;
@property(copy) NSString *engNozzle;
@end
@interface RocketBody : NSObject
{
NSNumber *fuel;
NSNumber *weight;
BOOL holdDownBolt_01;
BOOL holdDownBolt_02;
BOOL holdDownBolt_03;
BOOL holdDownBolt_04;
SolidRocketMotor *engine;
}
@property(copy) NSNumber *fuel;
@property(copy) NSNumber *weight;
@property BOOL holdDownBolt_01;
@property BOOL holdDownBolt_02;
@property BOOL holdDownBolt_03;
@pr开发者_StackOverflow中文版operty BOOL holdDownBolt_04;
@end
// ------------------------------------------------------------------- **
// IMPLEMENTATION
// ------------------------------------------------------------------- **
@implementation SolidRocketMotor
- (id) init {
self = [super init];
if (self) NSLog(@"_init: %@", NSStringFromClass([self class]));
return self;
}
@synthesize engCase;
@synthesize engPropellant;
@synthesize engIgniter;
@synthesize engNozzle;
- (void) dealloc {
NSLog(@"_deal: %@", NSStringFromClass([self class]));
[super dealloc];
}
@end
// ------------------------------------------------------------------- **
@implementation RocketBody
- (id) init {
self = [super init];
if (self) {
engine = [[SolidRocketMotor alloc] init];
NSLog(@"_init: %@", NSStringFromClass([self class]));
}
return self;
}
@synthesize fuel;
@synthesize weight;
@synthesize holdDownBolt_01;
@synthesize holdDownBolt_02;
@synthesize holdDownBolt_03;
@synthesize holdDownBolt_04;
- (void) dealloc {
NSLog(@"_deal: %@", NSStringFromClass([self class]));
[engine release], engine = nil;
[super dealloc];
}
@end
many thanks
gary
supposing you have something like and that you declare a property setting for SolidRocketMotor *engine:
RocketBody *rb = [[RocketBody alloc] init];
Then you would do this:
SolidRocketMotor *srm = [[SolidRocketMotor alloc] init];
rb.engine = srm;
and
rb.engine.engCase = whatever;
You need to provide an accessor for engine
in your RocketBody
class. Like this:
@interface RocketBody : NSObject
{
// ...
SolidRocketMotor *engine;
}
// ...
@property(retain) SolidRocketMotor *engine;
@end
@implementation RocketBody
// ...
@synthesize engine;
// ...
@end
With this, external code can access the engine
property:
// Obj-C 2.0 Property style:
ares_1x.engine.engCase = @"Standard";
// or setter/getter method style:
[[ares_1x engine] setEngCase:@"Standard"];
精彩评论