【深入浅出Objective-C】4.6协议(Protocols)和代理(Delegation)

1.为MoXieSword类添加协议,这里的协议可以理解为JAVA中的接口(interface)

#import "Sword.h"

@protocol MoXieUser;//添加协议
@interface MoXieSword : Sword{//继承
    id<MoXieUser> _delegate;//代理对象的变量
}
@property (nonatomic, assign) id<MoXieUser> delegate;

-(id)initWithProducerName:(NSString *)aProducerName produceAbility:(int)abilityValue;

-(void)love:(id)loveTarget;
-(void)attackPower:(id)hitTarget;

@end

//创建协议
@protocol MoXieUser <NSObject>

//必须
@required
-(void)wanJianGuiZongL:(MoXieSword *)aSword;
-(void)yiJinJing:(MoXieSword *)aSword;
-(void)love;

@optional
-(void)spaceStep:(MoXieSword *)aSword;

@end

#import "MoXieSword.h"

@implementation MoXieSword

-(id)initWithProducerName:(NSString *)aProducerName produceAbility:(int)abilityValue{
    return [self initWithName:@"莫邪" produceName:aProducerName producerAbility:abilityValue];
}

//重写override
-(id)initWithName:(NSString *)aWeaponName produceName:(NSString *)aProducerName producerAbility:(int)abilityValue{
    self = [super initWithName:aWeaponName produceName:aProducerName producerAbility:abilityValue];
    
    if(self){
        if(![_strName isEqualToString:@"莫邪"]){
            _strName = [[NSString alloc]initWithString:@"莫邪"];
        }
        _fLong = 1.2f;
        _fWeight = 20.0f;
    }
    return self;
}

//super initWithName 的时候会调用此重写的方法
-(void)initSwordAttr{
    NSLog(@"MoXieSword initSwordAttr...");
    _colorBody = [[NSColor darkGrayColor]retain];
    _materialBody = [[NSString alloc] initWithString:@"陨石"];
    
    //没有剑鞘
    _colorSheath = nil;
    _materialSheath = nil;
    
    _colorHandle = [[NSColor whiteColor]retain];
    _materialHandle = [[NSString alloc]initWithString:@"千年神木"];
}

-(void)attack:(id)hitTarget{
    if(!hitTarget){
        return;
    }
//    if([hitTarget respondsToSelector:@selector(receiveAttack:)]){//这里会执行一次。
//        [hitTarget performSelector:@selector(receiveAttack:) withObject:[NSNumber numberWithInt:[self attackValue] * 10]];
//    }
    //因为定义了NSObject+Enemy.h所有不需要再判断了
    [hitTarget performSelector:@selector(receiveAttack:) withObject:[NSNumber numberWithInt:[self attackValue] * 10]];
    
}

-(int)attackValue{
    switch (_quality) {
        case kExcellQuality:
            return 100000;
            break;
        default:
            return 700;
            break;
    }
}

//根据代理进行的修改
-(void)love:(id)loveTarget{
    if(_delegate){
        [_delegate love];
    }
}

-(void)attackPower:(id)hitTarget{
    if(_delegate){
        [_delegate wanJianGuiZongL:self];
        [_delegate yiJinJing:self];
        if([_delegate respondsToSelector:@selector(spaceStep:)]){
            [_delegate performSelector:@selector(spaceStep:) withObject:self];
            //[_delegate spaceStep:self];
        }
    }
}
@end

2.创建实现MoXieUser协议的一个类SwordMaster

#import "Sword.h"
#import "MoXieSword.h"

@interface SwordMaster : NSObject<MoXieUser>{
    MoXieSword *_moXie;
}

-(MoXieSword *)takeSword;

@end

#import "SwordMaster.h"

@implementation SwordMaster

-(MoXieSword *)takeSword{
    if(!_moXie){
        _moXie = [[MoXieSword alloc]initWithProducerName:@"coooliang" produceAbility:100];
        _moXie.delegate = self;
    }
    return _moXie;
}

//必须实现的方法
-(void)wanJianGuiZongL:(MoXieSword *)aSword{
    NSLog(@"万剑归宗");
}
-(void)yiJinJing:(MoXieSword *)aSword{
    NSLog(@"易筋经");
}
-(void)love{
    NSLog(@"I love you!");
}

//可不实现接口协议中的此方法
//-(void)spaceStep:(MoXieSword *)aSword;

@end

#import <Foundation/Foundation.h>
#import "Person.h"
#import "Weapon.h"
#import "Sword.h"
#import "MoXieSword.h"
#import "SwordMaster.h"

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        SwordMaster *master = [[SwordMaster alloc]init];
        MoXieSword *moxie = [master takeSword];
        
        [moxie attackPower:nil];
        [moxie love:nil];
        
    }
    return 0;
}
//输出结果:
2014-01-25 16:40:25.559 test[2410:303] MoXieSword initSwordAttr...
2014-01-25 16:40:25.560 test[2410:303] 万剑归宗
2014-01-25 16:40:25.561 test[2410:303] 易筋经
2014-01-25 16:40:25.561 test[2410:303] I love you!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值