简单例子让你很好的理解:协议与委托 (Protocol and Delegate)
2014-02-20 17:41
477 查看
1 协议:
协议,类似于Java或C#语言中的接口,它限制了实现类必须拥有哪些方法。它是对对象行为的定义,也是对功能的规范。
示例:
1 2 3 4 5 6 7 8 9 | // GoodChild.h #import <Foundation/Foundation.h> @protocol GoodChild <NSObject> -(void)filialPiety; @end |
1 2 3 4 5 6 7 8 | // Student.h #import <Foundation/Foundation.h> #import "GoodChild.h" //注意实现协议的语法。 @interface Student : NSObject<GoodChild> @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | // Student.m // protocol // // Created by sxt on 11-10-23. // Copyright 2011年 __MyCompanyName__. All rights reserved. // #import "Student.h" @implementation Student - (id)init { self = [super init]; if (self) { // Initialization code here. } return self; } -(void)filialPiety{ NSLog(@"孝敬父母。。"); } @end |
每个类虽只有一个父类,但可以实现多个协议,以逗号隔开便可。语法如下:
1 2 3 | @interface Student : NSObject<协议1,协议2> @end |
2 委托:
委托是objC中使用非常频繁的一种设计模式,它的实现与协议的使用是分不开的,让我们看一个综合示例:小公司老板日常的工作是管理公司、教导新员工、发工资与接电话。
其中管理公司、教导新员工是老板要亲为的。
而发工资与接电话老板希望招聘一个秘书来帮忙,于是对秘书的要求就是要略懂出纳发工资,要能帮助领导接电话。 而这两项要求便是协议,对类功能的限定。
1 2 3 4 5 6 7 8 9 10 11 | // SecProtocol.h #import <Foundation/Foundation.h> @protocol SecProtocol <NSObject> //发工资 -(void)payoff; //接电话 -(void)tel; @end |
1 2 3 4 5 6 7 | // Sec.h #import <Foundation/Foundation.h> #import "SecProtocol.h" @interface Sec : NSObject<SecProtocol> @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | // Sec.m #import "Sec.h" @implementation Sec - (id)init { self = [super init]; if (self) { // Initialization code here. } return self; } -(void)payoff{ NSLog(@"sec payoff"); } -(void)tel{ NSLog(@"sec tel"); } @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | // Boss.h #import <Foundation/Foundation.h> #import "SecProtocol.h" @interface Boss : NSObject //此属性用于指定秘书对象,此对象必须实现SecProtocol协议。 @property(nonatomic,retain) id<SecProtocol> detegate; //管理 -(void)manage; //教导新员工 -(void)teach; @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | // Boss.m #import "Boss.h" @implementation Boss @synthesize detegate=_detegate; - (id)init { self = [super init]; if (self) { // Initialization code here. } return self; } -(void)manage{ NSLog(@"boss manage"); } -(void)teach{ NSLog(@"boss teach"); } -(void)payoff{ NSAutoreleasePool *p=[[NSAutoreleasePool alloc] init]; [_detegate payoff]; [p release]; } -(void)tel{ NSAutoreleasePool *p=[[NSAutoreleasePool alloc] init]; [_detegate tel]; [p release]; } @end |
此时我们跟秘书对象就叫做代理对象,代理模式的名字由来于此。
最后调用测试下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | // main.m // delegate // // Created by sxt on 11-10-23. // Copyright 2011年 Jinlong Wei. All rights reserved. // #import <Foundation/Foundation.h> #import "Boss.h" #import "Sec.h" int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; //实例化老板对象 Boss *boss=[[[Boss alloc] init] autorelease]; //实例化秘书对象 Sec *sec=[[[Sec alloc] init] autorelease]; //设置老板的代理对象为秘书 boss.detegate=sec; //调用4个方法。 [boss payoff]; [boss tel]; [boss manage]; [boss teach]; [pool drain]; return 0; } |
(Protocol and Delegate) 实例解析
相关文章推荐
- WinForm中如何实现判断某些控件是否可用
- 反射望远镜的发展历程(4)
- Oracle监听有关的文件讲解
- C#防止sql注入 (转)
- open cv+C++错误及经验总结(四)
- c#中去掉字符串空格方法
- 淘宝内部大量使用的开源系统监控工具--Tsar
- SRM 601 D1 L2:WinterAndSnowmen, dp
- ZJU_BASICLEVEL_1012
- iOS 控件之 UIDatePicker
- python open 文件操作
- VS2010 常用快捷键
- C#控制定位Word光标移动到任意行或者最后一行,取得光标位置等操作
- android 开机logo
- 腾讯大规模Hadoop集群实践
- 反射望远镜的发展历程(3)
- iOS使用CoreMotion实现摇一摇功能
- 编程之美2.10 寻找数组的最大值和最小值
- #趣米杯#码农天气--码农自己的天气APP(三)
- [linux基础]linux的权限知识