多态练习

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

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;
}
原文地址:https://www.cnblogs.com/huen/p/3520152.html