@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 ;
}