OC僵尸 属性 便利构造器 BOOL类型 继承 方法重写

来源:互联网 发布:淘宝店铺运费模板 编辑:程序博客网 时间:2024/06/17 18:12

main.m


CommonZombie *cZomb = [CommonZombiecommonZombieWithHP:50withLose:3];

   while (1) { //循环被打

        [cZombloseHP];

       if ([cZomb die]) {//判断死亡跳出

           break;

        }

    }

    BarrierZombie *bZomb = [BarrierZombiebarrierZombieWithHP:80withLose:2withProp:@"路障"];

   while (1) {

        [bZombloseHP];

       if ([bZomb die]) {

           break;

        }

    }




CommonZombie.h


#import <Foundation/Foundation.h>


@interface CommonZombie :NSObject


@property(nonatomic,assign) NSInteger HP;

@property(nonatomic,assign) NSInteger lose;


- (instancetype)initWithHP:(NSInteger )hp withLose:(NSInteger )lose;

+ (instancetype)commonZombieWithHP:(NSInteger )hp withLose:(NSInteger )lose;

- (void)loseHP;

- (BOOL)die;

@end




CommonZombie.m


#import "CommonZombie.h"


@implementation CommonZombie


- (instancetype)initWithHP:(NSInteger )hp withLose:(NSInteger )lose {

   self = [superinit];

   if (self) {

       self.HP = hp;

       self.lose = lose;

    }

    return self;

}

+ (instancetype)commonZombieWithHP:(NSInteger )hp withLose:(NSInteger )lose {

   return [[CommonZombiealloc] initWithHP:hpwithLose:lose];

}

- (void)loseHP {

   if (self.HP -self.lose <0) {

       self.HP = 0;

    }else {

       self.HP -= self.lose;

    }

   NSLog(@"僵尸%ld",self.HP);

}

- (BOOL)die {

   if (self.HP <=0) {

       NSLog(@"die");

       return YES;

    }else {

       return NO;

    }

}


@end




BarrierZombie.h


#import "CommonZombie.h"


@interface BarrierZombie :CommonZombie


@property(nonatomic,retain) NSString *prop;


- (instancetype)initWithHP:(NSInteger)hp withLose:(NSInteger)lose withProp:(NSString *)prop;

+ (instancetype)barrierZombieWithHP:(NSInteger )hp withLose:(NSInteger )lose withProp:(NSString *)prop;


@end




BarrierZombie.m


- (instancetype)initWithHP:(NSInteger)hp withLose:(NSInteger)lose withProp:(NSString *)prop {

   self = [superinitWithHP:hp withLose:lose];

   if (self) {

       self.prop = prop;

    }

    return self;

}

+ (instancetype)barrierZombieWithHP:(NSInteger)hp withLose:(NSInteger)lose withProp:(NSString *)prop {

   return [[BarrierZombiealloc] initWithHP:hpwithLose:lose withProp:prop];

}

- (void)loseProp {

   NSLog(@"%@头盔掉了",self.prop);

}

- (void)loseHP {

   if (self.HP <=0) {

       self.HP = 0;

    }else {

       self.HP -= self.lose;

    }

   NSLog(@"%ld",self.HP);

}

- (BOOL)die {

   if (self.HP ==50) {

        [selfloseProp];

       return NO;

    }else if (self.HP <=0) {

       NSLog(@"DIE");

       return YES;

    }else {

       return NO;

    }

}


0 0