一、概念
二、代码
#import <Foundation/Foundation.h>
#pragma mark 类
#pragma mark - 2.枪
@interface Gun : NSObject
{
@public
int _bullet; // 子弹
}
- (void)shoot;
@end
@implementation Gun
- (void)shoot
{
if (_bullet > 0)
{
_bullet--;
NSLog(@"打了一枪 %i",_bullet);
}
else
{
NSLog(@"没有子弹了,请换弹夹");
}
}
@end
#pragma mark - 1.士兵
@interface Soldier : NSObject
{
@public
NSString *_name;
double _height;
double _weight;
}
- (void)fire:(Gun *)gun;
@end
@implementation Soldier
- (void)fire:(Gun *)g
{
[g shoot];
}
@end
#pragma mark - 3.弹夹
#pragma mark - main函数
int main(int argc, const char * argv[])
{
Soldier *s = [Soldier new];
s->_name = @"lyh";
s->_height = 1.71;
s->_weight = 65.0;
Gun *gp = [Gun new];
gp->_bullet = 10;
[s fire:gp]; // 地址
[s fire:gp];
[s fire:gp];
[s fire:gp];
[s fire:gp];
[s fire:gp];
return 0;
}