需求:1.定义普通僵尸类:实例变量:僵尸总血量、僵尸每次失血量。方法:初始化方法(总血量)、被打击失血、死亡。
2.定义路障僵尸类:实例变量:僵尸总血量、僵尸每次失血量,道具,弱点。方法:初始化方法(总血量)、被打击失血、失去装备、死亡。
3.定义铁桶僵尸类:实例变量:僵尸总血量、僵尸每次失血量,道具,弱点。方法:初始化方法(总血量)、被打击失血、失去装备、死亡。
4.在main.m中创建普通僵尸对象,设置总血量50,每次失血量为3,没有道具。
5.在main.m中创建路障僵尸对象,设置总血量80,每次失血量为2,设置道具为路障。
6.在main.m中创建铁桶僵尸对象,设置总血量120,每次失血量为1,设置道具为铁桶。
main.m
#import#import "IronDrumZombie.h"
#import "Zombie.h"
#import "RoadZombie.h"
int main(int argc, const char * argv[]) {
Zombie*zombie = [[Zombie alloc] initWithHP:50 lostHP:3];
[zombiehitted];
RoadZombie*road = [[RoadZombie alloc] initWithHP:80 lostHP:2 tool:@"路障"weakness:@"路痴"];
[roadhitted];
IronDrumZombie *iron = [[IronDrumZombie alloc] initWithHP:120lostHP:1 tool:@"铁桶" weakness:@"头部"];
[ironhitted];
return0;
}
定义普通僵尸、路障僵尸和铁桶僵尸三个类
Zombie.h
#import@interface Zombie : NSObject
{
NSIntegerHP;
NSIntegerlostHP;
}
- (id)initWithHP:(NSInteger)aHP lostHP:(NSInteger)aLostHP;
+ (Zombie *)zombieWithHP:(NSInteger)aHPlostHP:(NSInteger)aLostHP;
- (void)setHP:(NSInteger)aHP;
- (NSInteger)HP;
- (void)setLostHP:(NSInteger)aLostHP;
- (NSInteger)lostHP;
- (void)hitted;
- (void)death;
@end
Zombie.m
#import "Zombie.h"@implementation Zombie
- (id)initWithHP:(NSInteger)aHP lostHP:(NSInteger)aLostHP {
if (self =[super init]) {
HP = aHP;
lostHP = aLostHP;
}
returnself;
}
+ (Zombie *)zombieWithHP:(NSInteger)aHP lostHP:(NSInteger)aLostHP{
return[[Zombie alloc] initWithHP:aHP lostHP:aLostHP];
}
- (void)setHP:(NSInteger)aHP {
HP =aHP;
}
- (NSInteger)HP {
returnHP;
}
- (void)setLostHP:(NSInteger)aLostHP {
lostHP =aLostHP;
}
- (NSInteger)lostHP {
returnlostHP;
}
- (void)hitted {
while (HP> 0) {
if (lostHP > HP) {
lostHP = HP;
}
HP -= lostHP;
NSLog(@"僵尸掉血:%ld,剩余血量:%ld", lostHP, HP);
}
[selfdeath];
}
- (void)death {
NSLog(@"恭喜勇士,消灭恐怖的僵尸!!!你将赢得所有人的尊重!!!");
}
@end
RoadZombie.h
#import "Zombie.h"
@interface RoadZombie : Zombie
{
NSString*tool;
NSString*weakness;
}
- (id)initWithHP:(NSInteger)aHP lostHP:(NSInteger)aLostHPtool:(NSString *)aTool weakness:(NSString *)aWeakness;
+ (RoadZombie *)roadZombieWithHP:(NSInteger)aHPlostHP:(NSInteger)aLostHP tool:(NSString *)aTool weakness:(NSString*)aWeakness;
- (void)setTool:(NSString *)aTool;
- (NSString *)tool;
- (void)setWeakness:(NSString *)aWeakness;
- (NSString *)weakness;
- (void)lostProp;
@end
RoadZombie.m
#import "RoadZombie.h"
@implementation RoadZombie
- (id)initWithHP:(NSInteger)aHP lostHP:(NSInteger)aLostHPtool:(NSString *)aTool weakness:(NSString *)aWeakness {
self =[super initWithHP:aHP lostHP:aLostHP];
if (self){
tool = aTool;
weakness = aWeakness;
}
returnself;
}
+ (RoadZombie *)roadZombieWithHP:(NSInteger)aHPlostHP:(NSInteger)aLostHP tool:(NSString *)aTool weakness:(NSString*)aWeakness {
return[[RoadZombie alloc] initWithHP:aHP lostHP:aLostHP tool:aToolweakness:aWeakness];
}
- (void)setTool:(NSString *)aTool {
tool =aTool;
}
- (NSString *)tool {
returntool;
}
- (void)setWeakness:(NSString *)aWeakness {
weakness =aWeakness;
}
- (NSString *)weakness {
returnweakness;
}
- (void)hitted {
while (HP> 0) {
if (lostHP > HP) {
lostHP = HP;
}
HP -= lostHP;
NSLog(@"路障僵尸掉血:%ld,剩余血量:%ld", lostHP, HP);
[self lostProp];
}
[selfdeath];
}
- (void)lostProp {
if (HP <=30 && tool != nil) {
tool = nil;
lostHP *= 2;
NSLog(@"恭喜勇士打落路障僵尸装备!!!路障僵尸所受伤害翻倍!!!");
}
}
@end
IronDrumZombie.h
#import "Zombie.h"
@interface IronDrumZombie : Zombie
{
NSString *tool;
NSString *weakness;
}
- (id)initWithHP:(NSInteger)aHP lostHP:(NSInteger)aLostHPtool:(NSString *)aTool weakness:(NSString *)aWeakness;
+ (IronDrumZombie *)iornDrumZombieWithHP:(NSInteger)aHPlostHP:(NSInteger)aLostHP tool:(NSString *)aTool weakness:(NSString*)aWeakness;
- (void)setTool:(NSString *)aTool;
- (NSString *)tool;
- (void)setWeakness:(NSString *)aWeakness;
- (NSString *)weakness;
- (void)hitted;
- (void)lostProp;
- (void)death;
@end
IronDrumZombie.m
#import "IronDrumZombie.h"
@implementation IronDrumZombie
- (id)initWithHP:(NSInteger)aHP lostHP:(NSInteger)aLostHPtool:(NSString *)aTool weakness:(NSString *)aWeakness {
self =[super initWithHP:aHP lostHP:aLostHP];
if (self){
tool = aTool;
weakness = aWeakness;
}
returnself;
}
- (void)setTool:(NSString *)aTool {
tool =aTool;
}
- (NSString *)tool {
returntool;
}
- (void)setWeakness:(NSString *)aWeakness {
weakness =aWeakness;
}
- (NSString *)weakness {
returnweakness;
}
- (void)hitted {
while (HP> 0) {
if (lostHP > HP) {
lostHP = HP;
}
HP -= lostHP;
NSLog(@"铁桶僵尸掉血:%ld,剩余血量:%ld", lostHP, HP);
[self lostProp];
}
[selfdeath];
}
//nil:OC对象置空
//NULL:C语言指针置空
- (void)lostProp {
if (HP <=60 && tool != nil) {
tool = nil;
lostHP = 7;
NSLog(@"铁桶掉落!!!大家速度攻击啊!!!");
}
}
@end