黑马程序员--协议和代理
来源:互联网 发布:淘宝手机版怎么贷款 编辑:程序博客网 时间:2024/05/21 00:47
参考:http://blog.csdn.net/jiangwei0910410003/article/details/41777627
使用代理协议实现以下功能
//小孩类,护士类,保姆类,其中小孩类有两个方法:wash和play
//这里代理对象就是:护士类、保姆类,小孩类是被代理对象。
小孩接口文件
//// 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
- 黑马程序员--协议和代理
- 黑马程序员---oc 协议和代理
- 黑马程序员--Protocol 代理协议
- 黑马程序员--IOS学习笔记(协议和代理)
- 黑马程序员--OC-协议与代理设计
- 黑马程序员-OC学习笔记-----协议、代理
- 黑马程序员 --- OC中的协议代理
- 黑马程序员-代理和AOP
- 黑马程序员--静态代理和动态代理
- 黑马程序员--静态代理和动态代理
- 黑马程序员——ios开发基础之OC中的分类、协议、延展和代理
- 黑马程序员_ios基础总结11_协议,代理
- 黑马程序员 _10 OC基础 block 协议 代理
- 黑马程序员__protocol协议在代理模式中的使用
- 黑马程序员_ 利用oc的协议实现代理模式
- 黑马程序员——ObjC基础—协议与代理
- 黑马程序员------[Objective-C]协议-代理设计模式
- 黑马程序员——OC基础—-----协议与代理
- CSDN某分站SQL盲注漏洞(附poc)===== 时间延迟
- POJ 3006 Dirichlet's Theorem on Arithmetic Progressions
- 浅谈C++多态性
- The spatial index grid size is invalid 问题解决
- JavaScript 学习笔记(1):关于函数、对象以及面向对象
- 黑马程序员--协议和代理
- IntelliJ IDEA /Android Studio显示文档
- 揭秘LocalBroadcastManager实现原理
- 黑马程序员--Java概述
- android webview实现进度条效果
- IOS之7版本适配UITableVIew与UINavigationController
- linux之curses库
- 成员变量和局部变量
- 未铭图书策划中心征集计算机、互联网等书稿及作者