如何进行源文件的组织
代码中的类在定义中自然的拆分成接口和实现两个部分,一个.h文件存放接口部分的代码:类的@interface指令,公共的struct定义,enum常量,#define
和extern全局变量。头文件名与类名一致,只是后缀为.h。
所有的实现细节(类的@implementation,全局变量的定义,私有struct等)都放在与类同名,以.m为扩展名的文件中。
其中Cocoa.h包含了Foundation.h以及其他的一些东西。
这里面只有一个描述的方法。头文件包含“Tire.h”
Engine.m:
Car.h:
如果没有@class Tire;@class Engine;那么程序编译时候会出错,因为在car接口声明中用到了Tire和Engine类,在没有互相引用的前提下,一种解决办法是#import两个.h头文件,这样会引入很多信息,另一种是使用@class方式,因为它只是通过指针引用了Tire和Engine。@class创建了一个前向引用,就是在告诉编译器:相信我,以后你会知道这个类到底是什么,但是现在,你知道需要知道这些。
代码中的类在定义中自然的拆分成接口和实现两个部分,一个.h文件存放接口部分的代码:类的@interface指令,公共的struct定义,enum常量,#define
和extern全局变量。头文件名与类名一致,只是后缀为.h。
所有的实现细节(类的@implementation,全局变量的定义,私有struct等)都放在与类同名,以.m为扩展名的文件中。
将上面的car程序使用这样的原则拆分为:car.h,car.m;Tire.h,Tire.m;Engine.h,Engine.m;car_split.m.
Tire.h:
#import <Cocoa/Cocoa.h>
@interface Tire : NSObject
@end // Tire
其中Cocoa.h包含了Foundation.h以及其他的一些东西。
Tire.m:
#import "Tire.h"
@implementation Tire
- (NSString *) description
{
return (@"I am a tire. ");
} // description
@end // Tire
这里面只有一个描述的方法。头文件包含“Tire.h”
Engine.h:
#import <Cocoa/Cocoa.h>
@interface Engine : NSObject
@end // Engine
Engine.m:
#import "Engine.h"
@implementation Engine
- (NSString *) description
{
return (@"I am an engine!");
} // description
@end // Engine
Car.h:
#import <Cocoa/Cocoa.h>
@class Tire;
@class Engine;
@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
如果没有@class Tire;@class Engine;那么程序编译时候会出错,因为在car接口声明中用到了Tire和Engine类,在没有互相引用的前提下,一种解决办法是#import两个.h头文件,这样会引入很多信息,另一种是使用@class方式,因为它只是通过指针引用了Tire和Engine。@class创建了一个前向引用,就是在告诉编译器:相信我,以后你会知道这个类到底是什么,但是现在,你知道需要知道这些。
如果有循环依赖的关系,即A类使用B类,B类也使用A类。如果试图使用#import语句让这两个类相互引用,那么就会出现编译错误,但是如果在A.h使用@class B,B.h中使用@class A ,那么就可以了。巧妙使用@class指令能够减少编译时间。
car.m:
#import "Car.h"
#import "Engine.h"
#import "Tire.h"
@implementation Car
- (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
- (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
最后的主程序:
#import <Foundation/Foundation.h>
#import "Car.h"
#import "Tire.h"
#import "Engine.h"
int main (int argc, const char * argv[])
{
Car *car = [Car new];
int i;
for (i = 0; i < 4; i++) {
Tire *tire = [Tire new];
[car setTire: tire
atIndex: i];
}
Engine *engine = [Engine new];
[car setEngine: engine];
[car print];
return (0);
} // main