Objective-C:协议protocol
六、协议(protocol)
关键字:@optional、@required
(1)是一个类共享的一个方法列表
(2)它声明了一系列的方法而不进行实现
(3)遵从某个协议,就是需要实现协议中的方法
(4)类似于java的接口、C++中的纯虚函数
非正式协议、正式协议
重点:正式协议:所有实现正式协议的类, 都必须实现该协议中的所有方法
一个类可以实现任意多个协议
协议其实就是代理模式:自己不亲自做,让别人去做,遵循了协议的类,可以成为别的类的代理。
-----------------------------------------------------------------------------------------------------------------------------------------------------
//Boos类,制定委托协议并选择委托人(Boos有些事可以自己做,但是有些事需要委托人去做,例如秘书帮助老板去买票、订房、通知开会的人)
1 // Boos.h 2 // 04-Delegate 3 // 4 // Created by ma c on 15/8/12. 5 // Copyright (c) 2015年. All rights reserved. 6 // 7 8 #import <Foundation/Foundation.h> 9 @protocol BossDeledate 10 -(void)bookTicket; 11 -(void)bookRoom; 12 -(void)notify; 13 @end 14 @interface Boos : NSObject 15 @property(nonatomic,weak)id<BossDeledate> delegate; 16 -(void)travel; 17 -(void)meeting; 18 @end
1 // Boos.m 2 // 04-Delegate 3 // 4 // Created by ma c on 15/8/12. 5 // Copyright (c) 2015年. All rights reserved. 6 // 7 8 #import "Boos.h" 9 10 @implementation Boos 11 -(void)travel 12 { 13 //1、订机票 14 [_delegate bookTicket]; 15 //2、订酒店 16 [_delegate bookRoom]; 17 //3、去出差 18 NSLog(@"Boss travel"); 19 } 20 -(void)meeting 21 { 22 //1、通知开会的所有人 23 [_delegate notify]; 24 //2、讲话 25 NSLog(@"Boss talk"); 26 } 27 @end
//选取person作为委托人
1 // Person.h 2 // 04-Delegate 3 // 4 // Created by ma c on 15/8/12. 5 // Copyright (c) 2015年. All rights reserved. 6 // 7 8 #import <Foundation/Foundation.h> 9 #import "Boos.h" 10 @interface Person : NSObject<BossDeledate> 11 12 @end
1 // Person.m 2 // 04-Delegate 3 // 4 // Created by ma c on 15/8/12. 5 // Copyright (c) 2015年 bjsxt. All rights reserved. 6 // 7 8 #import "Person.h" 9 10 @implementation Person 11 -(void)bookTicket 12 { 13 NSLog(@"person book ticket"); 14 } 15 -(void)bookRoom 16 { 17 NSLog(@"person book room"); 18 } 19 -(void)notify 20 { 21 NSLog(@"person notify all people meeting!"); 22 } 23 @end
//还可以选取Student作为代理人
1 // Student.h 2 // 04-Delegate 3 // 4 // Created by ma c on 15/8/12. 5 // Copyright (c) 2015年. All rights reserved. 6 // 7 8 #import <Foundation/Foundation.h> 9 #import "Boos.h" 10 @interface Student : NSObject <BossDeledate> 11 12 @end
1 // Student.m 2 // 04-Delegate 3 // 4 // Created by ma c on 15/8/12. 5 // Copyright (c) 2015年 bjsxt. All rights reserved. 6 // 7 8 #import "Student.h" 9 10 @implementation Student 11 -(void)bookTicket 12 { 13 NSLog(@"student book ticket"); 14 } 15 -(void)bookRoom 16 { 17 NSLog(@"student book room"); 18 } 19 -(void)notify 20 { 21 NSLog(@"student notify all people meeting!"); 22 } 23 @end
//测试代理人
1 // main.m 2 // 04-Delegate 3 // 4 // Created by ma c on 15/8/12. 5 // Copyright (c) 2015年. All rights reserved. 6 // 7 8 #import <Foundation/Foundation.h> 9 #import "Boos.h" 10 #import "Person.h" 11 #import "Student.h" 12 int main(int argc, const char * argv[]) 13 { 14 @autoreleasepool 15 { 16 //测试代理模式 17 Boos *boss = [[Boos alloc]init]; 18 Person *person = [[Person alloc]init]; 19 Student *student = [[Student alloc]init]; 20 21 //老板设置代理 22 [boss setDelegate:person]; 23 24 [boss meeting]; 25 [boss travel]; 26 27 28 //老板重新选择学生作为代理人 29 [boss setDelegate:student]; 30 31 [boss meeting]; 32 [boss travel]; 33 } 34 return 0; 35 }
//运行结果
2015-08-12 15:04:23.663 04-Delegate[1223:70726] person notify all people meeting! 2015-08-12 15:04:23.664 04-Delegate[1223:70726] Boss talk 2015-08-12 15:04:23.664 04-Delegate[1223:70726] person book ticket 2015-08-12 15:04:23.665 04-Delegate[1223:70726] person book room 2015-08-12 15:04:23.665 04-Delegate[1223:70726] Boss travel 2015-08-12 15:04:23.665 04-Delegate[1223:70726] student notify all people meeting! 2015-08-12 15:04:23.665 04-Delegate[1223:70726] Boss talk 2015-08-12 15:04:23.665 04-Delegate[1223:70726] student book ticket 2015-08-12 15:04:23.665 04-Delegate[1223:70726] student book room 2015-08-12 15:04:23.666 04-Delegate[1223:70726] Boss travel Program ended with exit code: 0
程序猿神奇的手,每时每刻,这双手都在改变着世界的交互方式!