对象作为方法的参数传递

/*
 士兵
 事物名称: 士兵(Soldier)
 属性:姓名(name), 身高(height), 体重(weight)
 行为:打枪(fire), 打电话(callPhone)

 枪
 事物名称:枪(Gun)
 属性:弹夹(clip) , 型号(model)
 行为:上弹夹(addClip)

 弹夹
 事物名称: 弹夹(Clip)
 属性:子弹(Bullet)
 行为:上子弹(addBullet)
 */


#pragma mark - 枪
@interface Gun : NSObject
{
    @public
    int _bullet;    // 子弹
}
- (void)shoot;

@end

@implementation Gun

- (void)shoot
{
    if (_bullet > 0) {
        _bullet--;
        NSLog(@"子弹还剩余%d个", _bullet);
    }else
    {
        NSLog(@"枪里没有子弹");
    }
}

@end

#pragma mark - 士兵
@interface Solier : NSObject
{
    @public
    NSString *_name;
    double _height;
    double _weight;
}

- (void)fire:(Gun *)gun;
@end

@implementation Solier

- (void)fire:(Gun *)gun
{
    [gun shoot];
}

@end

int main(int argc, const char * argv[]) {

    Solier *solier = [Solier new];
    solier->_name = @"大兵";
    solier->_height = 1.88;
    solier->_weight = 130.33;

    Gun *gun = [Gun new];
    gun->_bullet = 10;

    [solier fire:gun];
    [solier fire:gun];
    [solier fire:gun];
    [solier fire:gun];

    return 0;
}

你可能感兴趣的:(Objective)