// // main.m // CAR3 // // Created by jimzhai on 13-1-24. // Copyright (c) 2013年 jimzhai. All rights reserved. // #import <Foundation/Foundation.h> #import "Car.h" int main(int argc, const char * argv[]) { Car *car = [Car new]; [car print]; Engine *engine2 = [Engine new]; [engine2 setBrand:@"兰博基尼1026"]; [car setEngine:engine2]; Tire *newTire = [Tire new]; [newTire setBrand:@"永久"]; [car setTire:newTire atIndex:2]; [car print]; return 0; }
// // Car.h // CAR3 // // Created by jimzhai on 13-1-24. // Copyright (c) 2013年 jimzhai. All rights reserved. // #import <Foundation/Foundation.h> #import "Engine.h" #import "Tire.h" @interface Car : NSObject { Engine *engine; Tire *tire[4]; } - (void) print; - (void) setTire: (Tire*)tire atIndex: (int)index; - (Tire*)tireAtIndex: (int)index; - (void)setEngine: (Engine*)engine; - (Engine*)getEngine; @end//Car// // Car.m // CAR3 // // Created by jimzhai on 13-1-24. // Copyright (c) 2013年 jimzhai. All rights reserved. // #import "Car.h" @implementation Car - (id) init { if(self = [super init]) { engine = [Engine new]; [engine setBrand:@"法拉利10016"]; tire[0] = [Tire new]; tire[1] = [Tire new]; tire[2] = [Tire new]; tire[3] = [Tire new]; for(int i = 0 ; i < 4 ; i++) [tire[i] setBrand:@"米其林"]; } return self; }//init - (void) print { NSLog(@"%@" , engine); NSLog(@"%@" , tire[0]); NSLog(@"%@" , tire[1]); NSLog(@"%@" , tire[2]); NSLog(@"%@" , tire[3]); }// print - (void) setTire:(Tire *)_tire atIndex: (int) index { if(index < 0 || index > 3) { NSLog(@"Index is wrong!"); exit(1); } tire[index] = _tire; }//setTire - (Tire*) tireAtIndex:(int)index { if(index<0||index>3) { NSLog(@"index is wrong"); exit(1); } return tire[index]; }//tireAtIndex - (void)setEngine:(Engine *)_engine { engine = _engine; }//setEngine - (Engine*)getEngine { return engine; }//getEngine @end//Car
// // Engine.h // CAR3 // // Created by jimzhai on 13-1-24. // Copyright (c) 2013年 jimzhai. All rights reserved. // #import <Foundation/Foundation.h> @interface Engine : NSObject { NSString *brand; } - (void) setBrand: (NSString*) _brand; - (NSString*) getBrand; @end
// // Engine.m // CAR3 // // Created by jimzhai on 13-1-24. // Copyright (c) 2013年 jimzhai. All rights reserved. // #import "Engine.h" @implementation Engine - (NSString*) description { return [NSString stringWithFormat:@"I am engine ! my brand is %@",brand]; }//description - (void) setBrand:(NSString *)_brand { brand = _brand; }//setBrand - (NSString*) getBrand { return brand; }//getBrand @end//Engine
// // Tire.h // CAR3 // // Created by jimzhai on 13-1-24. // Copyright (c) 2013年 jimzhai. All rights reserved. // #import <Foundation/Foundation.h> @interface Tire : NSObject { NSString *brand; } - (void) setBrand: (NSString*) brandl; - (NSString*) getBrand; @end//Tire
// // Tire.m // CAR3 // // Created by jimzhai on 13-1-24. // Copyright (c) 2013年 jimzhai. All rights reserved. // #import "Tire.h" @implementation Tire - (NSString*) description { return [NSString stringWithFormat:@"I am a tire. My brand is %@",brand]; }//description - (void) setBrand:(NSString *)brandl { brand = brandl ; }//setBrand -(NSString*) getBrand { return brand; }//getBrand @end//Tire