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

it2022-05-05  147

在.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 }

 

 

转载于:https://www.cnblogs.com/zhengguoli/p/3314357.html


最新回复(0)