您的位置:首页 > 移动开发 > Objective-C

Objective-C的.h与.m文件中公有和私有函数的声明和定义方式[学习笔记]

2013-09-11 13:23 603 查看
在.h文件中,

  @interface与@end之间声明的是公有的方法
在.m文件中,

  @interface与@end之间声明的是私有的方法(注意:与在.h中声明不同,类名要有"()",后面不用写继承的基类);

  @implementation与@end之间定义公有或私有的方法

例如:

Spaceship.h

#import "Vehicle.h"
#import "Planet.h"
@interface Spacship : Vehicle
//declaration of public methods
//.h文件中,@interface与@end之间声明的是公有的方法

@property (nonatomic) double topSpeed;

- (void) orbitPlanet:(Planet *)aPlanet atAltitude:(double)km;

@end


Spaceship.m

#import "Spaceship.h"
@interface Spaceship()
//declaration of private methods (as needed)
//.m文件中,@interface与@end之间声明的是私有的方法(注意:与在.h中声明不同,类名要有"()",后面不用写继承的基类)
@property (nontoxic, strong) Warmhole *nearestWormhole;
@end

@implementation Spaceship
//implementation of public and private methods
//@implementation与@end之间定义公有或私有的方法
@synthesize topSpeed=_topSpeed;
@synthesize nearestWormhole=_nearestWormhole;

- (void) setTopSpeed:(double)speed
{
_topSpeed=speed;
}

- (double) topSpeed
{
return _topSpeed;
}

- (void) orbitPlanet:(Planet *)aPlanet atAltitude:(double)km
{
//put the code to orbit a planet here
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐