黑马程序员--协议和代理

参考:http://blog.csdn.net/jiangwei0910410003/article/details/41777627

使用代理协议实现以下功能

//小孩类,护士类,保姆类,其中小孩类有两个方法:washplay

//这里代理对象就是:护士类、保姆类,小孩类是被代理对象。


小孩接口文件

//
//  Children.h
//  分类
//
//  Created by smartlei on 15/6/17.
//  Copyright (c) 2015年 smartlei. All rights reserved.
//

#import <Foundation/Foundation.h>

@class Children;//声明Children是一个类

@protocol ChildrenDelegate <NSObject>//声明要发布代理

    @required//声明遵循代理的必须要实现以下方法
    -(void)wash:(Children *)children;
    -(void)play:(Children *)children;

@end


@interface Children : NSObject

@property id <ChildrenDelegate> delegate;//小孩儿的托管对象:既然是对象就能调用其方法
@property    NSInteger timeValue;


@end

小孩类实现文件

//
//  Children.m
//  分类
//
//  Created by smartlei on 15/6/17.
//  Copyright (c) 2015年 smartlei. All rights reserved.
//

#import "Children.h"
#import "Nure.h"
#import "Nanny.h"



@implementation Children

@synthesize delegate,timeValue;

-(id)init
{
    
    if (self=[super init])
    {
        [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
    }
    
    return self;
}

-(void)timerAction:(NSTimer *)timer
{
   
    Nanny *nanny=[Nanny new];//保姆实例
    Nure *nure=[Nure new];//护士实例
    timeValue++;
    if (timeValue==5)
    {
        [self setDelegate:nure];//给小孩找护士
        [delegate wash:self];
        
    }
    else if(timeValue==10)
    {
        timeValue=0;
         [self setDelegate:nanny];//给小孩找保姆
        [delegate play:self];
    }
}

@end

护士类接口部分:

#import <Foundation/Foundation.h>
#import "Children.h"//导入小孩基类

@interface Nure : NSObject <ChildrenDelegate>//遵循代理协议

-(void)wash:(Children *)children;
-(void)play:(Children *)children;


@end

护士类实现部分

#import "Nure.h"

@implementation Nure


-(void)wash:(Children *)children
{
    NSLog(@"护士  在给小孩洗澡");
}
-(void)play:(Children *)children
{
     NSLog(@"护士  陪小孩玩耍");
}
@end


保姆类接口部分

#import <Foundation/Foundation.h>
#import "Children.h"
@interface Nanny : NSObject <ChildrenDelegate>

-(void)wash:(Children *)children;
-(void)play:(Children *)children;
@end

保姆类实现部分

#import "Nanny.h"

@implementation Nanny

-(void)wash:(Children *)children
{
    NSLog(@"保姆  在给小孩洗澡");
}
-(void)play:(Children *)children
{
    NSLog(@"保姆  陪小孩玩耍");
}
@end

主函数测试文件

#include <stdio.h>
#import "Children.h"
#import "Nure.h"
#import "Nanny.h"

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

    
    Children *chil=[[Children alloc] init];
  //  Nanny *nanny=[Nanny new];//保姆实例
    Nure *nure=[Nure new];//护士实例
     [chil setDelegate:nure];//给小孩找护士
    [[NSRunLoop currentRunLoop] run];
    
    return 0;
}

输出部分:

2015-06-17 17:26:11.171 协议和代理[898:44319] 护士  在给小孩洗澡

2015-06-17 17:26:16.173 协议和代理[898:44319] 保姆  陪小孩玩耍

2015-06-17 17:26:21.172 协议和代理[898:44319] 护士  在给小孩洗澡

2015-06-17 17:26:26.168 协议和代理[898:44319] 保姆  陪小孩玩耍

2015-06-17 17:26:31.169 协议和代理[898:44319] 护士  在给小孩洗澡

2015-06-17 17:26:36.171 协议和代理[898:44319] 保姆  陪小孩玩耍

2015-06-17 17:26:41.169 协议和代理[898:44319] 护士  在给小孩洗澡

2015-06-17 17:26:46.173 协议和代理[898:44319] 保姆  陪小孩玩耍

2015-06-17 17:26:51.169 协议和代理[898:44319] 护士  在给小孩洗澡

2015-06-17 17:26:56.169 协议和代理[898:44319] 保姆  陪小孩玩耍










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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值