xcode4.2
未了把代码看清楚,拆分
main.m:
//
// main.m
// carDemo
//
// Created by Wunderman on 12-1-3.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Engine.h"
#import "Tire.h"
#import "Car.h"
int 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;
}
//
// Engine.h
// carDemo
//
// Created by Wunderman on 12-1-3.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Engine : NSObject
@end
//
// Engine.m
// carDemo
//
// Created by Wunderman on 12-1-3.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import "Engine.h"
@implementation Engine
- (NSString *) description {
return (@"I am a engine. Vrooom");
}
@end
//
// Tire.h
// 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
//
// Tire.m
// carDemo
//
// Created by Wunderman on 12-1-3.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import "Tire.h"
@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
//
// Car.h
// carDemo
//
// Created by Wunderman on 12-1-3.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import <Foundation/Foundation.h>
@class Engine;
@class Tire;
@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.m
// carDemo
//
// Created by Wunderman on 12-1-3.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import "Car.h"
@implementation Car
- (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;
}
- (void) print {
NSLog(@"%@", engine);
NSLog(@"%@", tires[0]);
NSLog(@"%@", tires[1]);
NSLog(@"%@", tires[2]);
NSLog(@"%@", tires[3]);
}
@end