//
// CarObject.h
// Car
//
// Created by on 14-8-19.
// Copyright (c) 2014年 com.car. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Tire : NSObject
@end // Tire
@interface Engine : NSObject
@end // Engine
@interface Car : NSObject
{
Engine *engine;
Tire *tires[4];
}
- (void) print;
@end // Car
//
// CarObject.m
// Car
//
// Created by on 14-8-19.
// Copyright (c) 2014年 com.car. All rights reserved.
//
#import "CarObject.h"
@implementation Tire
- (NSString *) description {
return (@"I am a tire. I last a while.");
} // description
@end // Tire
@implementation Engine
- (NSString *) description {
return (@"I am an engine. Vrooom!");
} // description
@end // Engine
@implementation Car
// 返回的是id类型,id即为泛型对象指针
- (id) init {
if (self = [super init]) {
engine = [Engine new];
tires[0] = [Tire new];
tires[1] = [Tire new];
tires[2] = [Tire new];
tires[3] = [Tire new];
}
return (self);
} // init
- (void) print {
NSLog(@"%@", engine);
NSLog(@"%@", tires[0]);
NSLog(@"%@", tires[1]);
NSLog(@"%@", tires[2]);
NSLog(@"%@", tires[3]);
} // print
@end // Car
//
// main.m
// Car
//
// Created by on 14-8-19.
// Copyright (c) 2014年 com.car. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "CarObject.h"
int main(int argc, const char * argv[])
{
Car *car;
car = [Car new];
[car print];
return 0;
} // main
运行结果:
I am an engine. Vrooom!
I am a tire. I last a while.
I am a tire. I last a while.
I am a tire. I last a while.
I am a tire. I last a while.
重新设计的Car程序(复合):
//
// Car.h
// newCar
//
// Created by on 14-8-19.
// Copyright (c) 2014年 com.newCar. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Engine.h"
#import "Tire.h"
@interface Car : NSObject
{
Engine *engine;
Tire *tires[4];
}
- (Engine *) engine;
- (void) setEngine: (Engine *) newEngine;
- (Tire *) tireAtIndex: (int) index;
- (void) setTire: (Tire *) tire atIndex: (int) index;
- (void) print;
@end // Car
//
// Car.m
// newCar
//
// Created by on 14-8-19.
// Copyright (c) 2014年 com.newCar. All rights reserved.
//
#import "Car.h"
@implementation Car
- (Engine *)engine {
return (engine);
} // engine
- (void) setEngine:(Engine *)newEngine {
engine = newEngine;
} // setEngine:
- (void) setTire:(Tire *)tire atIndex:(int)index {
if (index < 0 || index > 3) {
NSLog(@"bad index (%d) in setTire: atIndex:", index);
exit(1);
}
tires[index] = tire;
} // setTire:atIndex:
- (Tire *) tireAtIndex:(int)index {
if (index < 0 || index > 3) {
NSLog(@"bad index (%d) in tireAtIndex:", index);
exit(1);
}
return (tires[index]);
} // tireAtIndex:
- (void) print {
NSLog(@"%@", engine);
NSLog(@"%@", tires[0]);
NSLog(@"%@", tires[1]);
NSLog(@"%@", tires[2]);
NSLog(@"%@", tires[3]);
} // print
@end // Car
//
// Tire.h
// newCar
//
// Created by on 14-8-19.
// Copyright (c) 2014年 com.newCar. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Tire : NSObject
@end
//
// Tire.m
// newCar
//
// Created by on 14-8-19.
// Copyright (c) 2014年 com.newCar. All rights reserved.
//
#import "Tire.h"
@implementation Tire
- (NSString *)description {
return (@"I am a tire. I last a while");
} // description
@end // Tire
//
// Engine.h
// newCar
//
// Created by on 14-8-19.
// Copyright (c) 2014年 com.newCar. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Engine : NSObject
@end
//
// Engine.m
// newCar
//
// Created by on 14-8-19.
// Copyright (c) 2014年 com.newCar. All rights reserved.
//
#import "Engine.h"
@implementation Engine
- (NSString *)description {
return (@"I am an engine. Vrooom!");
} // description
@end // Engine
//
// main.m
// newCar
//
// Created by on 14-8-19.
// Copyright (c) 2014年 com.newCar. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Engine.h"
#import "Tire.h"
#import "Car.h"
int main(int argc, const char * argv[]) {
// new 一个car的对象
Car *car = [Car new];
// new 一个engin的对象
Engine *engine = [Engine new];
// 调用engin的setEngine的方法
[car setEngine: engine];
for (int i = 0; i < 4; i++) {
Tire *tire = [Tire new];
// 调用car的setTire:atIndex:方法,并传入两个参数
[car setTire:tire atIndex:i];
}
[car print];
return 0;
}
运行结果:
I am an engine. Vrooom!
I am a tire. I last a while.
I am a tire. I last a while.
I am a tire. I last a while.
I am a tire. I last a while.