// // Engine.h // carParts // // Created by 110 on 10-1-24. // Copyright 2010 __MyCompanyName__. All rights reserved. // #import///** *实现nscopying协议,可以进行复制 */ @interface Engine : NSObject { } @end
// Engine.m // carParts // // Created by 110 on 10-1-24. // Copyright 2010 __MyCompanyName__. All rights reserved. // #import "Engine.h" @implementation Engine /** 采用NSCopying协议,必须实现copyWithZone:方法,zone是NSZone类的一个对象。 指向一块可供分配的内存区域,当你向一个对象发送copy消息时,该copy消息在到达你的代码之前被转换为copyWithZone:方法。 */ -(id) copyWithZone:(NSZone *) Zone{ Engine * engineCopy; /** allocWithZone:是一个实例方法,我们需要将该消息发送给一个类,而不是一个实例变量 allocWithZone: 分配内存并创建一个该类的新对象,init消息使其初始化 **/ engineCopy=[[[self class] allocWithZone:Zone]init]; return (engineCopy); } -(NSString*) description{ return (@"i am a engine,vroomal"); } @end
// // Tire.h // carParts // // Created by 110 on 10-1-24. // Copyright 2010 __MyCompanyName__. All rights reserved. // #import//@interface Tire : NSObject { } @end
// Tire.m // carParts // // Created by 110 on 10-1-24. // Copyright 2010 __MyCompanyName__. All rights reserved. // #import "Tire.h" @implementation Tire - (id) copyWithZone:(NSZone *)zone{ Tire *tireCopy; tireCopy=[[[self class] allocWithZone:zone] init]; return (tireCopy); } -(NSString*) description{ return (@"i am a tire,i last a while"); } @end
//
// Car.h // carParts // // Created by 110 on 10-1-24. // Copyright 2010 __MyCompanyName__. All rights reserved. // #import@class Tire; @class Engine; //@class 创建一个向前引用,就是这告诉编译器:“相信我,以后你会知道这个类到底是什么,但是现在,你只需要知道这些” @interface Car : NSObject { Tire *tires[4]; Engine *engine; } -(void) setEngine:(Engine*) newEngine; -(Engine *)engine; -(void) setTire:(Tire*) tire atIndex:(int) index; -(Tire*) tireAtIndex:(int) index; -(void) print; @end
//
// Car.m // carParts // // Created by 110 on 10-1-24. // Copyright 2010 __MyCompanyName__. All rights reserved. // #import "Car.h" #import "Tire.h" #import "Engine.h" @implementation Car -(id) copyWithZone:(NSZone *) zone{ Car *carCopy; carCopy=[[[self class] allocWithZone:zone] init ]; //carCopy.name=self.name; Engine *engineCopy; engineCopy=[[Engine copy] autorelease]; carCopy.engine=engineCopy; int i; for(i=0;i<4;i++){ Tire *tireCopy; tireCopy=[[self tireAtIndex:i] copy]; [tireCopy autorelease]; [carCopy setTire:tireCopy atIndex:i]; } return (carCopy); } -(void) setEngine:(Engine *) newEngine{ engine=newEngine; } -(Engine *) engine{ return (engine); } -(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; } -(Tire *) tireAtIndex:(int) index{ if(index<0||index>3){ NSLog(@"bad index (%d) in setTire:atIndex:",index); exit(1); } return tires[index]; } -(void) print{ NSLog(@"-----%@",tires[0]); NSLog(@"00000=%@",tires[1]); NSLog(@"%@",tires[2]); NSLog(@"%@",tires[3]); } @end
//
// Slant6.h // carParts // // Created by 110 on 10-1-24. // Copyright 2010 __MyCompanyName__. All rights reserved. // #import#import "Engine.h" //它是继承其他类而不是通过指针指向其他类,所以不能这头文件里使用@class语句。因为编译器需要先知道所有关于超类的信息才能成功地为其子类编译@interface. @interface Slant6 : Engine { } @end
//
// Slant6.m // carParts // // Created by 110 on 10-1-24. // Copyright 2010 __MyCompanyName__. All rights reserved. // #import "Slant6.h" @implementation Slant6 -(id) copyWithZone:(NSZone *) zone{ Slant6 *slant6Copy; slant6Copy=[super copyWithZone:zone] ; return (slant6Copy); } -(NSString *) description{ return (@" i am a slant-6"); } @end
// // AllWeatherRadia.h // carParts // // Created by 110 on 10-1-24. // Copyright 2010 __MyCompanyName__. All rights reserved. // #import#import "Tire.h" @interface AllWeatherRadia : Tire { } @end
//
// AllWeatherRadia.m // carParts // // Created by 110 on 10-1-24. // Copyright 2010 __MyCompanyName__. All rights reserved. // #import "AllWeatherRadia.h" @implementation AllWeatherRadia -(id) copyWithZone:(NSZone *) zone{ AllWeatherRadia *allWeatherRadiaCopy; allWeatherRadiaCopy=[super copyWithZone:zone]; return (allWeatherRadiaCopy); } -(NSString *) description{ return (@" i am a tire for rain or shine "); } @end