OC的类

来源:互联网 发布:联合国数据库中文版 编辑:程序博客网 时间:2024/06/03 19:47
#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];}@endint 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;}

0 0
原创粉丝点击