// 对象作为参数传递
//好好感受一下面向对象的思想,枪有射击的功能,士兵使用枪射击,就给士兵一个枪,然后调用枪的射击方法就可以了
#import <Foundation/Foundation.h>
#pragma mark - 枪
//枪
//事物名称:枪(Gun)
//属性:弹夹(clip) , 型号(model)
//行为:射击
@interface Gun : NSObject
{
@public
int _bullet; // 子弹数
int _model;
}
- (void)shoot; // 射击
@end
@implementation Gun
- (void)shoot{
if (_bullet>0) {
NSLog(@"射击了一次,还剩%i个子弹",--_bullet);
}
}
@end
#pragma mark - 士兵
//士兵
//事物名称: 士兵(Soldier)
//属性:姓名(name), 身高(height), 体重(weight)
//行为:打枪(fire), 打电话(callPhone)
@interface Soldier : NSObject
{
@public
NSString *_name;
float _height;
float _weight;
}
- (void)fire:(Gun*)mGun;
- (void)call;
@end
@implementation Soldier
-(void)fire:(Gun *)mGun{
[mGun shoot];
}
-(void)call{
NSLog(@"士兵在打电话");
}
@end
int main(int argc, const char * argv[]) {
// 创建士兵对象
Soldier *ps = [Soldier new];
// 初始化士兵对象
ps->_name=@"瑞恩";
ps->_height=1.88;
ps->_weight=100;
// 创建枪对象
Gun *pg=[Gun new];
// 初始化枪对象
pg->_bullet=10;
pg->_model=47;
// 调用射击方法
[ps fire:pg];
[ps fire:pg];
[ps fire:pg];
[ps fire:pg];
[ps fire:pg];
return 0;
}