在继承这一部分,我觉得比较难理解的就是各种初始化方法, 便利构造器等,在这个练习中,比较充分的运用了这几种方法。
首先是各种类的创建:
然后是僵尸类中的成员变量的定义,初始化,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