Objective-C学习 继承之僵尸练习

  在继承这一部分,我觉得比较难理解的就是各种初始化方法, 便利构造器等,在这个练习中,比较充分的运用了这几种方法。

  首先是各种类的创建:


  然后是僵尸类中的成员变量的定义,初始化,setter、getter方法,被攻击方法:

@interface Corpse : NSObject

{

    NSString *_type;

//    int _blood;

//    int _loseblood;

    int _maxHP;

    int _currentHP;  // 当前血量

    int _loseHP;

    BOOL _isDeath;

}


- (id)initWithType:(NSString *)type

             MaxHP:(int)maxHP

         CurrentHP:(int)currentHP

            LoseHP:(int)loseHP

           IsDeath:(BOOL)isdeath;


//  setter getter

- (void)setType:(NSString *)type;

- (NSString *)type;



- (void)setMaxHP:(int)maxHP;

- (int)maxHP;


- (void)setCurrentHP:(int)currentHP;

- (int)currentHP;


- (void)setLoseHP:(int)loseHP;

- (int)loseHP;


- (void)setIsDeath:(BOOL)isDeath;

- (BOOL)isDeath;


//  方法


- (int)attack;


实现:

- (id)initWithType:(NSString *)type

             MaxHP:(int)maxHP

         CurrentHP:(int)currentHP

            LoseHP:(int)loseHP

           IsDeath:(BOOL)isdeath

{

    self = [super init];

    if (self) {

        _type = type;

        _maxHP = maxHP;

        _currentHP = currentHP;

        _loseHP = loseHP;

        _isDeath = isdeath;

    }

    return self;

}


- (void)setType:(NSString *)type

{

    _type = type;

}

- (NSString *)type

{

    return _type;

}



- (void)setMaxHP:(int)maxHP

{

    _maxHP = maxHP;

}

- (int)maxHP

{

    return _maxHP;

}


- (void)setCurrentHP:(int)currentHP

{

    _currentHP = currentHP;

}

- (int)currentHP

{

    return _currentHP;

}


- (void)setLoseHP:(int)loseHP

{

    _loseHP = loseHP;

}

- (int)loseHP

{

    return _loseHP;

}


- (void)setIsDeath:(BOOL)isDeath

{

    _isDeath = isDeath;

}

- (BOOL)isDeath

{

    return _isDeath;

}


- (int)attack

{

    _currentHP -= _loseHP;

    if (_currentHP <= 0) {

        _isDeath = YES;

        _currentHP = 0;

    }

    return _currentHP;

}


 因为防具僵尸是继承于僵尸类,所以其中的声明方法等就不详细陈述,主要是对攻击方法的重写:

- (int)attack

{

    

    if (_currentHP <= _maxHP / 2) {

        _loseHP = 3;

    }

     _currentHP -= _loseHP;

    if (_currentHP <= 0) {

        _isDeath = YES;

        _currentHP = 0;

    }

    

    return _currentHP;

}


  同理,铁通僵尸继承于防具僵尸,所以对攻击的重写:

- (int)attack

{

    if (_currentHP <= _maxHP / 3) {

        _loseHP = 3;

       

    }

    _currentHP -= _loseHP;

    if (_currentHP <= 0) {

        _isDeath = YES;

        _currentHP = 0;

    }


    return _currentHP;

}

  在主函数中分别创建三个对象:普通僵尸,防具僵尸,铁桶僵尸:

        Corpse *a = [[Corpse alloc] initWithType:@"普通" MaxHP:50 CurrentHP:50 LoseHP:3 IsDeath:NO];

        FJCorpse *b = [[FJCorpse alloc] initWithType:@"路障" MaxHP:80 CurrentHP:80 LoseHP:2 IsDeath:NO Fjtype:@"路障"];

        IronCorpse *c = [[IronCorpse alloc] initWithType:@"铁通" MaxHP:120 CurrentHP:120 LoseHP:1 IsDeath:NO Fjtype:@"铁桶" Weekness:@"吸铁石"];


 最后实现对三只僵尸的同时攻击,我这里想的是三只僵尸分别站在三条路上, 下面是代码实现:

        int i = 0;

        while (![a isDeath]) {

            [a attack];

            i++;

        }

        NSLog(@"普通僵尸脑袋掉了");

        NSLog(@"攻击次数:%d", i);

        

        int j = 0;

        while (![b isDeath]) {

             [b attack];

              j++;

            if ([b currentHP] == [b maxHP] / 2) {

                NSLog(@"%d次攻击后路障消失", j);

            }

         }

        

        NSLog(@"路障僵尸脑袋掉了");

        NSLog(@"攻击次数:%d", j);

        int k = 0;

        while (![c isDeath]) {

            [c attack];

            k++;

            if ([c currentHP] == [c maxHP] / 3) {

                NSLog(@"%d次攻击后铁桶被打掉", j);

            }

        }

        NSLog(@"铁桶僵尸脑袋掉了");

        NSLog(@"攻击次数:%d", k);

    }


  最后输出的结果是:

2014-09-16 16:37:59.818 Homework3 test2[1443:303] 普通僵尸脑袋掉了

2014-09-16 16:37:59.820 Homework3 test2[1443:303] 攻击次数:17

2014-09-16 16:37:59.821 Homework3 test2[1443:303] 20次攻击后路障消失

2014-09-16 16:37:59.821 Homework3 test2[1443:303] 路障僵尸脑袋掉了

2014-09-16 16:37:59.821 Homework3 test2[1443:303] 攻击次数:34

2014-09-16 16:37:59.822 Homework3 test2[1443:303] 34次攻击后铁桶被打掉

2014-09-16 16:37:59.822 Homework3 test2[1443:303] 铁桶僵尸脑袋掉了

2014-09-16 16:37:59.822 Homework3 test2[1443:303] 攻击次数:94




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值