多态练习

需求:修改游戏相关类 将怪物类和英雄类提取公共部分,并定义成父类。 修改游戏,使用多态完善怪物、英雄的攻击。

Role.h

多态练习
@interface Role : NSObject
{
    int _hp;
    int _oriHp;
    CGPoint _currentLoc;
    CGPoint _oriLoc;
    int _attack;
    int _defense;
}

@property(nonatomic,assign)int hp;
@property(nonatomic,assign,readonly)int oriHp;
@property(nonatomic,assign)CGPoint currentLoc;
@property(nonatomic,assign,readonly)CGPoint oriLoc;
@property(nonatomic,assign)int attack;
@property(nonatomic,assign)int defense;

-(Role *)initWithHp:(int)aHp
         CurrentLoc:(CGPoint)aCurrentLoc
             Attack:(int)aAttack
            Defense:(int)aDefense;

+(Role *)RoleWithHp:(int)aHp
         CurrentLoc:(CGPoint)aCurrentLoc
             Attack:(int)aAttack
            Defense:(int)aDefense;

-(void)move;
-(void)attackSomebody:(Role *)aRole;
-(void)escape;
@end
多态练习

Role.m

多态练习
#import "Role.h"

@implementation Role
@synthesize hp = _hp,
            oriLoc = _oriLoc,
            oriHp = _oriHp,
            currentLoc = _currentLoc,
            attack = _attack,
            defense = _defense;

-(Role *)initWithHp:(int)aHp
         CurrentLoc:(CGPoint)aCurrentLoc
             Attack:(int)aAttack
            Defense:(int)aDefense
{
    self = [super init];
    if (self)
    {
        CGPoint p = {100,100};
        _oriLoc = p;
        _oriHp = 100;
        
        self.hp = aHp;
        self.currentLoc = aCurrentLoc;
        self.attack = aAttack;
        self.defense = aDefense;
    }
    return self;
}

+(Role *)RoleWithHp:(int)aHp
         CurrentLoc:(CGPoint)aCurrentLoc
             Attack:(int)aAttack
            Defense:(int)aDefense
{
    Role *r = [[Role alloc]init];
    return r;
}

-(void) move
{
    CGPoint p = self.currentLoc;
    p.x++;
    p.y++;
}
-(void) attackSomebody:(Role *)aRole
{
    
}
-(void) escape
{
      NSLog(@"逃跑");
}
@end
多态练习

Hero.h

多态练习
#import "Role.h"

@interface Hero : Role
{
    NSString *_name;
}
@property(nonatomic,retain)NSString *name;

-(Role *)initWithHp:(int)aHp
         CurrentLoc:(CGPoint)aCurrentLoc
             Attack:(int)aAttack
            Defense:(int)aDefense
               Name:(NSString *)aName;

+(Role *)RoleWithHp:(int)aHp
         CurrentLoc:(CGPoint)aCurrentLoc
             Attack:(int)aAttack
            Defense:(int)aDefense
               Name:(NSString *)aName;
@end
多态练习

Hero.m

多态练习
#import "Hero.h"
#import "Monster.h"
@implementation Hero
@synthesize name = _name;

-(Role *)initWithHp:(int)aHp
         CurrentLoc:(CGPoint)aCurrentLoc
             Attack:(int)aAttack
            Defense:(int)aDefense
               Name:(NSString *)aName
{
    self = [super initWithHp:aHp
                    CurrentLoc:aCurrentLoc
                    Attack:aAttack
                    Defense:aDefense];
    if (self)
    {
        self.name = aName;
    }
    return self;
}

+(Role *)RoleWithHp:(int)aHp
         CurrentLoc:(CGPoint)aCurrentLoc
             Attack:(int)aAttack
            Defense:(int)aDefense
               Name:(NSString *)aName
{
    Role *r = [[Role alloc]init];
    return r;
}

-(void)attackSomebody:(Role *)aRole
{
    Monster *aMonster =(Monster *)aRole;
    aMonster.hp -= self.attack;
    NSLog(@"monster hp = %d",aMonster.hp);
}
@end
多态练习

Monster.h

#import "Role.h"

@interface Monster : Role

@end

Monster.m

多态练习
#import "Monster.h"
#import "Hero.h"
@implementation Monster

-(void)attackSomebody:(Role *)aRole
{
    Hero *aHero = (Hero *)aRole;
    aHero.hp -= self.attack;
    NSLog(@"hero hp = %d",aHero.hp);
}

@end
多态练习

AppDelegate.m

多态练习
#import "AppDelegate.h"
#import "Role.h"
#import "Monster.h"
#import "Hero.h"
@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    
    CGPoint p = {100,100};
    Role *hero = [[Hero alloc]initWithHp:100
                               CurrentLoc:p
                                   Attack:30
                                  Defense:80
                                     Name:@"superman"];

    Role *monster = [[Monster alloc]initWithHp:100
                                    CurrentLoc:p
                                        Attack:20
                                       Defense:30];
    [hero attackSomebody:monster];
    [monster attackSomebody:hero];
    
    
    [self.window makeKeyAndVisible];
    return YES;
}
多态练习

多态练习

上一篇:Java 日期格式化工具类


下一篇:菜根谭#52