OC的类

#import <Foundation/Foundation.h>

typedef enum{
    kSoldierLevel1,
    kSoldierLevel2,
    kSoldierLevel3
}SoldierLevel;

@interface Gun : NSObject
{
    @public
    int _bulletCount;
}

- (void)shoot;

@end

@implementation Gun

- (void)shoot
{
    if (_bulletCount > 0) {
        _bulletCount--;
        NSLog(@"砰");
    }else{
        NSLog(@"子弹打完了");
    }
}

@end

@interface Shop : NSObject

- (Gun *)buyGun;

- (void)buyBullet:(Gun *)gun;

@end

@implementation Shop

- (Gun *)buyGun
{
    Gun *gun = [Gun new];
    // 买枪送子弹
    [self buyBullet:gun];
    return gun;
}

- (void)buyBullet:(Gun *)gun
{
    gun->_bulletCount = 1;
}

@end

@interface Soldier : NSObject
{
    @public
    NSString *_name;
    SoldierLevel _level;
}

- (void)fireWithGun:(Gun *)gun;

@end

@implementation Soldier

- (void)fireWithGun:(Gun *)gun
{
    NSLog(@"士兵%@开枪射击", _name);
    [gun shoot];
}

@end


int main(int argc, const char * argv[])
{
    Shop *shop = [Shop new];
    Gun *gun = [shop buyGun];
    
    Soldier *soldier = [Soldier new];
    soldier->_name = @"ShuaiGeqi";
    soldier->_level = kSoldierLevel1;
    
    [soldier fireWithGun:gun];
    
    [soldier fireWithGun:gun];
    
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted on 2015-10-26 21:17  ll_xyls  阅读(111)  评论(0编辑  收藏  举报