@class關鍵字
如要需要匯入某類的功能代碼則需要包含這個類的.h檔案,如果只需要知道這個東西是個類(如某檔案中如下聲明: Person *person; )使用@class就可以了(@class Person;)。正確的使用@class指令能夠減少編譯時間。
源檔案組織樣本:
//// Tire.h// car4//// Created by Elf Sundae on 10-10-20.// Copyright 2010 __MyCompanyName__. All rights reserved.//#import <Cocoa/Cocoa.h>@interface Tire : NSObject@end
//// Tire.m// car4//// Created by Elf Sundae on 10-10-20.// Copyright 2010 __MyCompanyName__. All rights reserved.//#import "Tire.h"@implementation Tire- (NSString *) description{return(@"I am a Tire");}@end
//// Engine.h// 汽車引擎//// Created by Elf Sundae on 10-10-20.// Copyright 2010 __MyCompanyName__. All rights reserved.//#import <Cocoa/Cocoa.h>@interface Engine : NSObject@end
//// Engine.m// ////// Created by Elf Sundae on 10-10-20.// Copyright 2010 __MyCompanyName__. All rights reserved.//#import "Engine.h"@implementation Engine- (NSString *) description{return (@"I am a Engine");}@end
//// V8.h// car4//// Created by Elf Sundae on 10-10-20.// Copyright 2010 __MyCompanyName__. All rights reserved.//#import <Cocoa/Cocoa.h>#import "Engine.h"// *******************// V8 Engine Class@interface V8 : Engine@end
//// V8.m// car4//// Created by Elf Sundae on 10-10-20.// Copyright 2010 __MyCompanyName__. All rights reserved.//#import "V8.h"@implementation V8- (NSString *) description{return @"I am a V8 Engine!";}@end // V8
//// Car.h// car4//// Created by Elf Sundae on 10-10-20.// Copyright 2010 __MyCompanyName__. All rights reserved.//#import <Cocoa/Cocoa.h>@class Engine;@class Tire;@interface Car : NSObject{Engine *engine;Tire *tires[4];}// 添加getter,setter- (Engine *) engine;- (void) setEngine:(Engine *) m_engine;- (Tire *) tireAtIndex: (int) index;//通過索引器訪問該屬性- (void) setTire: (Tire *) m_tire : (int) index;- (void) print;@end
//// Car.m// car4//// Created by Elf Sundae on 10-10-20.// Copyright 2010 __MyCompanyName__. All rights reserved.//#import "Car.h"#import "Engine.h"#import "Tire.h"@implementation Car//- (id) init// 初始化Car//{//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;//}- (Engine *) engine{return engine;}- (void) setEngine:(Engine *)m_engine{engine = m_engine;}- (Tire *) tireAtIndex:(int)index{if (index < 0 || index > 3){NSLog(@"bad index (%d) in \"tireAtIndex:\"", index);exit(1);}return tires[index];}- (void) setTire:(Tire *)m_tire :(int)index{if (index < 0 || index >3){NSLog(@"bad index (%d) in \"setTire:atIndex\"", index);exit(1);}tires[index] = m_tire;}- (void) print{NSLog(@"%@",engine);NSLog(@"%@",tires[0]);NSLog(@"%@",tires[1]);NSLog(@"%@",tires[2]);NSLog(@"%@",tires[3]);}@end
//// WeatherRadial.h// car4//// Created by Elf Sundae on 10-10-20.// Copyright 2010 __MyCompanyName__. All rights reserved.//#import <Cocoa/Cocoa.h>#import "Tire.h"// *******************// WeatherRadial Tire@interface WeatherRadial : Tire@end
//// WeatherRadial.m// car4//// Created by Elf Sundae on 10-10-20.// Copyright 2010 __MyCompanyName__. All rights reserved.//#import "WeatherRadial.h"@implementation WeatherRadial- (NSString *) description{return (@"I am a WeatherRadial Tire!!");}@end
#import <cocoa/Cocoa.h>#import "Engine.h"// 因為V8.h和WeatherRadial.h中#import "Tire.h"// 已經包含Engine.h和Tire.h,此處可不用匯入#import "Car.h"#import "V8.h"#import "WeatherRadial.h"/* * car4: 聯絡組織專案檔 * */int main (int argc, const char * argv[]) {Car *car;car = [Car new];V8 *engine = [V8 new];[car setEngine: engine];int i;for (i = 0; i < 4; i++){WeatherRadial *tire = [WeatherRadial new];[car setTire:tire :i];}[car print];return 0 ;}