2、定義路障殭屍類: 實例變量:殭屍種類、殭屍總血量、殭屍每次失血量,道具,弱點。方法:初始化方法(設置殭屍種類,總血量)、被打擊失血、失去裝備、死亡。

@interface BarricadeZombie : NSObject
{
    NSString *_species;    //種類
    NSInteger _totalVolume;   //總血量
    NSInteger _everyTimeLoss;  //每次失血量
    NSString *_prop;     //道具
    NSString *_weakness;    //弱點
}
- (NSString *)species;
- (NSInteger)totalVolume;
- (NSInteger)everyTimeLoss;
//初始化
- (id)initWithSpecies:(NSString *)species totalVolume:(NSInteger)totalVolume everyTimeLoss:(NSInteger)everyTimeLoss;
//剩餘血量
- (NSInteger)bloodLoss;
////失去道具
//- (void)lossOfEquioment;
//死亡
- (void)death;
@end
@implementation BarricadeZombie
- (id)initWithSpecies:(NSString *)species totalVolume:(NSInteger)totalVolume everyTimeLoss:(NSInteger)everyTimeLoss
{
    _species = species;
    _totalVolume = totalVolume;
    _everyTimeLoss = everyTimeLoss;
    return self;
}
- (NSString *)species
{
    return _species;
}
- (NSInteger)totalVolume
{
    return _totalVolume;
}
- (NSInteger)everyTimeLoss
{
    return _everyTimeLoss;
}


- (NSInteger)bloodLoss
{
    NSInteger i = 0;
    while (_totalVolume > 0) {
        if (_totalVolume > 50) {
            _totalVolume -= _everyTimeLoss;
        }else{
            _totalVolume -= 3;
        }
        i++;
    }
    return i;
}
- (void)death
{
    NSLog(@"死亡");
}
@end

BarricadeZombie *ba = [[BarricadeZombie alloc] initWithSpecies:@"路障殭屍" totalVolume:80 everyTimeLoss:2];
        NSLog(@"%@,%ld滴血,每次攻擊掉%ld滴血",[ba species],[ba totalVolume],[ba everyTimeLoss]);
        NSLog(@"殭屍來襲,準備攻擊");
        NSLog(@"被打%ld之後",[ba bloodLoss]);
        [ba death];


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章