OC继承:打僵尸
#import <Foundation/Foundation.h>
@interface Jiangshi :NSObject
{
NSString *_type;
NSInteger _totleBloodValue;//总血量
NSInteger _reduceBloodValue;//被攻击时减少的血量
int _lastBloodValue;//剩余血量
bool _isDeath;//判断死亡状态
}
-(id)initWithNameType:(NSString *)type
totileBloodValue:(NSInteger)totileBloodValue
reduceBloodValue:(NSInteger)reduceBloodValue;
+(id)jiangshiWithType:(NSString *)type
totileBloodValue:(NSInteger)totileBloodValue
reduceBloodValue:(NSInteger)reduceBloodValue;
-(void)setType:(NSString *)type;
-(void)setTotleBloodValue:(NSInteger)totleBloodValue;
-(void)setReduceBloodValue:(NSInteger)reduceBloodValue;
-(NSString *)type;
-(NSInteger)totleBloodValue;
-(NSInteger)reduceBloodValue;
- (BOOL)isDeath;
- (void)death;
- (int)byAttackReduceBloodValue;
@end
#import "Jiangshi.h"
@implementation Jiangshi
-(id)initWithNameType:(NSString *)type
totileBloodValue:(NSInteger)totileBloodValue
reduceBloodValue:(NSInteger)reduceBloodValue
{
self = [superinit];
if (self) {
_type = type;
_totleBloodValue = totileBloodValue;
_reduceBloodValue = reduceBloodValue;
}
return self;
}
+(id)jiangshiWithType:(NSString *)type
totileBloodValue:(NSInteger)totileBloodValue
reduceBloodValue:(NSInteger)reduceBloodValue
{
Jiangshi *i = [[Jiangshialloc]
initWithNameType:type totileBloodValue:totileBloodValuereduceBloodValue:reduceBloodValue];
return i;
}
-(void)setType:(NSString *)type
{
_type = type;
}
-(void)setTotleBloodValue:(NSInteger)totleBloodValue
{
_totleBloodValue = totleBloodValue;
}
-(void)setReduceBloodValue:(NSInteger)reduceBloodValue
{
_reduceBloodValue = reduceBloodValue;
}
-(NSString *)type
{
return _type;
}
-(NSInteger)totleBloodValue
{
return_totleBloodValue;
}
-(NSInteger)reduceBloodValue
{
return_reduceBloodValue;
}
- (BOOL)isDeath
{
return_lastBloodValue<=0;
}
- (void)death
{
NSLog(@"已死");
}
- (int)byAttackReduceBloodValue
{
_lastBloodValue -=_reduceBloodValue;
return_lastBloodValue;
}
@end
#import "Jiangshi.h"
@interface Daojujiangshi :Jiangshi
{
NSString *_daoju;
}
-(id)initWithNameType:(NSString *)type
totileBloodValue:(NSInteger)totileBloodValue
reduceBloodValue:(NSInteger)reduceBloodValue
daoju:(NSString *)daoju;
+(id)daojujiangshiWithType:(NSString *)type
totileBloodValue:(NSInteger)totileBloodValue
reduceBloodValue:(NSInteger)reduceBloodValue
daoju:(NSString *)daoju;
- (BOOL)isloseProp;
- (void)setReduceBloodValue:(int)reduceBloodValue;
- (int)byAttackReduceBloodValue;
@end
@implementation Daojujiangshi
-(id)initWithNameType:(NSString *)type
totileBloodValue:(NSInteger)totileBloodValue
reduceBloodValue:(NSInteger)reduceBloodValue
daoju:(NSString *)daoju
{
self = [superinitWithNameType:type totileBloodValue:totileBloodValuereduceBloodValue:reduceBloodValue];
if (self) {
_daoju = daoju;
}
return self;
}
+(id)daojujiangshiWithType:(NSString *)type
totileBloodValue:(NSInteger)totileBloodValue
reduceBloodValue:(NSInteger)reduceBloodValue
daoju:(NSString *)daoju
{
Daojujiangshi *d = [[Daojujiangshialloc]
initWithNameType:type totileBloodValue:totileBloodValuereduceBloodValue:reduceBloodValuedaoju:daoju];
return d;
}
- (BOOL)isloseProp
{
return_lastBloodValue<(_totleBloodValue*0.5);
}
- (int)byAttackReduceBloodValue
{
if ([self isloseProp]) {
_lastBloodValue -=_reduceBloodValue ;
}else
_lastBloodValue -=(_reduceBloodValue-1) ;
return_lastBloodValue;
}
@end
#import "Daojujiangshi.h"
@interface Tietongjiangshi :Daojujiangshi
{
NSString *_weakness;
}
-(id)initWithNameType:(NSString *)type
totileBloodValue:(NSInteger)totileBloodValue
reduceBloodValue:(NSInteger)reduceBloodValue
daoju:(NSString *)daoju
weakness:(NSString *)weakness;
+(id)tietongjiangshiWithType:(NSString *)type
totileBloodValue:(NSInteger)totileBloodValue
reduceBloodValue:(NSInteger)reduceBloodValue
daoju:(NSString *)daoju
weakness:(NSString *)weakness;
- (BOOL)isloseProp;
- (BOOL)isWeakness;
- (int)byAttackReduceBloodValue;
@end
#import "Tietongjiangshi.h"
@implementation Tietongjiangshi
-(id)initWithNameType:(NSString *)type
totileBloodValue:(NSInteger)totileBloodValue
reduceBloodValue:(NSInteger)reduceBloodValue
daoju:(NSString *)daoju
weakness:(NSString *)weakness
{
self = [superinitWithNameType:type totileBloodValue:totileBloodValuereduceBloodValue:reduceBloodValuedaoju:daoju];
if (self) {
_weakness = weakness;
}
return self;
}
+(id)tietongjiangshiWithType:(NSString *)type
totileBloodValue:(NSInteger)totileBloodValue
reduceBloodValue:(NSInteger)reduceBloodValue
daoju:(NSString *)daoju
weakness:(NSString *)weakness
{
Tietongjiangshi *z = [[Tietongjiangshialloc]
initWithNameType:type totileBloodValue:totileBloodValuereduceBloodValue:reduceBloodValuedaoju:daoju weakness:weakness];
return z;
}
- (BOOL)isloseProp
{
return_lastBloodValue<=(_lastBloodValue*0.3);
}
- (int)byAttackReduceBloodValue
{
if ([self isloseProp]) {
_lastBloodValue -=_reduceBloodValue;
}else
_lastBloodValue -=_reduceBloodValue-2;
return_lastBloodValue;
}
@end
int main(int argc,const char * argv[])
{
@autoreleasepool {
Jiangshi *i = [JiangshijiangshiWithType:@"普通僵尸"totileBloodValue:50reduceBloodValue:3];
// [i byAttackReduceBloodValue];
Daojujiangshi *d = [DaojujiangshidaojujiangshiWithType:@"道具僵尸"totileBloodValue:80reduceBloodValue:2 daoju:@"飞镖"];
Tietongjiangshi *z = [TietongjiangshitietongjiangshiWithType:@"铁桶僵尸"totileBloodValue:120reduceBloodValue:1 daoju:@"飞镖" weakness:@"头部"];
// [z byAttackReduceBloodValue];
while (1) {
int lastBloodValue = [i byAttackReduceBloodValue];
NSLog(@"剩余血量:%d",lastBloodValue);
if ([i isDeath]){
[ideath];
break;
}
}
while (1) {
int lastBloodValue = [d byAttackReduceBloodValue];
NSLog(@"剩余血量:%d",lastBloodValue);
if ([d isDeath]){
[ddeath];
break;
}
}
while (1) {
int lastBloodValue = [z byAttackReduceBloodValue];
NSLog(@"剩余血量:%d",lastBloodValue);
if ([z isDeath]){
[zdeath];
break;
}
}
return 0;
}
}