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

来源:互联网 发布:淘宝手机版怎么贷款 编辑:程序博客网 时间:2024/05/21 00:47

参考: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
原创粉丝点击