1、定义普通僵尸类: 实例变量:僵尸种类、僵尸总血量、僵尸每次失血量。 方法:初始化方法(设置僵尸种类,总血量)、被打击失血、死亡。

@interface OrdinaryZombie : NSObject
{
    NSString *_species;    //种类
    NSInteger _totalVolume;   //总血量
    NSInteger _everyTimeLoss;  //每次失血量
}
//初始化
- (id)initWithSpecies:(NSString *)species totalVolume:(NSInteger)totalVolume;
- (void)seteveryTimeLoss:(NSInteger)everyTimeLoss;
//读取
- (NSString *)species;
- (NSInteger)totalVolume;
- (NSInteger)everyTimeLoss;

//被打击失血
- (NSInteger)bloodLoss;
- (void)death;
@end

@implementation OrdinaryZombie
- (id)initWithSpecies:(NSString *)species totalVolume:(NSInteger)totalVolume
{
    _species = species;
    _totalVolume = totalVolume;
    return self;
}
- (NSString *)species
{
    return _species;
}
- (NSInteger)totalVolume
{
    return _totalVolume;
}

- (void)seteveryTimeLoss:(NSInteger)everyTimeLoss
{
    _everyTimeLoss = everyTimeLoss;
}
- (NSInteger)everyTimeLoss
{
    return _everyTimeLoss;
}

- (NSInteger)bloodLoss
{
    NSInteger i = 0;
    while (_totalVolume > 0) {
        _totalVolume -= _everyTimeLoss;
        i++;
    }
    return i;
}
- (void)death
{
    NSLog(@"僵尸死亡");
}
@end
@autoreleasepool {
        OrdinaryZombie *or = [[OrdinaryZombie alloc] initWithSpecies:@"普通僵尸" totalVolume:50];
        NSLog(@"%@,%ld",[or species],[or totalVolume]);
        NSLog(@"僵尸来袭,准备攻击");
        [or seteveryTimeLoss:3];
        NSLog(@"被打击掉血%ld",[or everyTimeLoss]);
        NSLog(@"被打击%ld次之后",[or bloodLoss]);
        [or death];





你可能感兴趣的:(类,实例,interface)