obj c car 未拆分(此程序不符合内存管理规则)
xcode4.2,未拆分程序,都在一个文件内
//// main.m// carDemo//// Created by Wunderman on 12-1-3.// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.//#import <Foundation/Foundation.h>@interface Tire : NSObject { int index;}- (int) index;- (void) setIndex : (int) i;@end@implementation Tire- (int) index { return index; };- (void) setIndex : (int) i { index = i;}- (NSString *) description { //return (@"I am a tire. i'am in %d", index); NSString* sFormat = [[NSString alloc] initWithFormat: @"I am a tire. i'am in %d", index]; NSString* string = [[NSString alloc] initWithString: sFormat]; return string;}@end //Tire impl@interface Engine : NSObject @end@implementation Engine- (NSString *) description { return (@"I am a engine. Vrooom");}@end //Engine impl@interface Car : NSObject { Engine *engine; Tire *tires[4];}//engine's get set- (Engine *) engine;- (void) setEngine : (Engine *) newEngine;//tire's get set- (Tire *)tireAtIndex: (int) index;- (void) setTire: (Tire *) tire atIndex: (int) index;- (void) print;@end // Car inter@implementation Car/*- (id)init { self = [super init]; if (self) { engine = [[Engine alloc] init]; tires[0] = [[Tire alloc] init]; tires[1] = [[Tire alloc] init]; tires[2] = [[Tire alloc] init]; tires[3] = [[Tire alloc] init]; } return self;}*/- (Engine *) engine { return engine;}- (void) setEngine:(Engine *) newEngine { engine = newEngine;}- (Tire *)tireAtIndex: (int) index { if (index >3 || index < 0) { NSLog(@"bad index %d", index); exit(1); } return tires[index];}- (void) setTire: (Tire *) tire atIndex: (int) index { if (index >3 || index < 0) { NSLog(@"bad index %d", index); exit(1); } tires[index] = tire;}// car impl- (void) print { NSLog(@"%@", engine); NSLog(@"%@", tires[0]); NSLog(@"%@", tires[1]); NSLog(@"%@", tires[2]); NSLog(@"%@", tires[3]);}@end// Carint main (int argc, const char * argv[]){ @autoreleasepool { // insert code here... NSLog(@"Hello, World!"); Car *car = [[Car alloc] init]; Engine *engine = [[Engine alloc] init]; [car setEngine: engine]; int i; for (i=0; i<4; i++) { Tire *tire = [[Tire alloc] init]; [tire setIndex: i]; [car setTire:tire atIndex:i]; } [car print]; } return 0;}??