在下面的代码中,设置了两种不同的协议规则:一种是老师对学生设置的协议:即老师发出命令后,学生站起来、回答问题、坐下; 另一种是我对学生设置的协议:即学生按照我的协议中的初始化函数去初始化一个整数。
//我设置的协议Myprotocol,里面有我设置的协议规则(属性、函数)作为一个单独的文件
1 // Myprotocol.h 2 // 协议 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 10 @protocol Myprotocol 11 @property(nonatomic,assign)NSInteger integer; 12 -(id)initWithInteger:(NSInteger) i; 13 -(void) show2; 14 -(void)print; 15 -(void) initialize; 16 @end
//老师设置的协议和类本身的属性 .h和.m文件;在类里面直接设置协议,没有作为单独的文件
1 // Teacher.h 2 // 协议 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 TeacherDelegate//老师制定协议规则 10 @required //在协议代理人类中必须要全部实现的方法 11 -(void) show1; 12 -(void) Standup; 13 -(void) Answerquestion; 14 @optional //协议代理人类中可以选择性的实现(可以实现,也可以不用实现) 15 -(void) Setdown; 16 @end 17 18 19 @interface Teacher : NSObject 20 @property(nonatomic,weak) id<TeacherDelegate> delegate; 21 -(void)ordering; 22 @end
1 // Teacher.m 2 // 协议 3 // 4 // Created by ma c on 15/8/12. 5 // Copyright (c) 2015年. All rights reserved. 6 // 7 8 #import "Teacher.h" 9 10 @implementation Teacher 11 @synthesize delegate; 12 -(void)ordering 13 { 14 [delegate show1]; 15 NSLog(@"teacher is ordering!"); 16 [self.delegate Standup]; 17 [self.delegate Answerquestion]; 18 [self.delegate Setdown]; 19 } 20 @end
//学生类Student 它作为实现这两种协议的代理人(委托者).h和.m文件
1 // Student.h 2 // 协议 3 // 4 // Created by ma c on 15/8/12. 5 // Copyright (c) 2015年. All rights reserved. 6 // 7 8 #import "Teacher.h" 9 #import "TeacherDelegate.h" 10 #import "Myprotocol.h" 11 @interface Student : Teacher<TeacherDelegate,Myprotocol> 12 @end
1 // Student.m 2 // 协议 3 // 4 // Created by ma c on 15/8/12. 5 // Copyright (c) 2015年. All rights reserved. 6 // 7 8 #import "Student.h" 9 10 @implementation Student 11 @synthesize integer; 12 //TeacherDelegate 13 -(void) show1 14 { 15 NSLog(@"the teacher's protocol is running!"); 16 } 17 -(void) Standup 18 { 19 NSLog(@"student stand up."); 20 } 21 -(void) Answerquestion 22 { 23 NSLog(@"student answer question."); 24 } 25 -(void) Setdown 26 { 27 NSLog(@"student set down!"); 28 } 29 //Myprotocol 30 -(void) show2 31 { 32 NSLog(@"the my protocol is running!"); 33 } 34 -(id)initWithInteger:(NSInteger) i 35 { 36 self = [super init]; 37 if(self) 38 { 39 self.integer = i; 40 } 41 return self; 42 } 43 -(void)print 44 { 45 NSLog(@"integer=%ld",self.integer); 46 } 47 -(void) initialize 48 { 49 NSLog(@"Integer's initialization succeed."); 50 } 51 @end
//主函数测试这两种协议的实现
1 // main.m 2 // 协议 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 "Teacher.h" 10 #import "Student.h" 11 #import "Myprotocol.h" 12 int main(int argc, const char * argv[]) 13 { 14 @autoreleasepool 15 { 16 Teacher *teacher = [[Teacher alloc]init]; 17 18 //老师设置学生代理实现老师设置的协议(TeacherDelegate) 19 Student *student = [[Student alloc]init]; 20 [teacher setDelegate:student]; 21 [teacher ordering]; 22 printf("\n"); 23 24 //我设置学生代理实现我设置的协议(Myprotocol) 25 Student *stu = [[Student alloc]initWithInteger:10]; 26 [stu show2]; 27 [stu print]; 28 [stu initialize]; 29 } 30 return 0; 31 }
//运行结果
2015-08-12 19:55:51.498 协议[1965:139749] the teacher's protocol is running! 2015-08-12 19:55:51.499 协议[1965:139749] teacher is ordering! 2015-08-12 19:55:51.499 协议[1965:139749] student stand up. 2015-08-12 19:55:51.500 协议[1965:139749] student answer question. 2015-08-12 19:55:51.500 协议[1965:139749] student set down! 2015-08-12 19:55:51.500 协议[1965:139749] the my protocol is running! 2015-08-12 19:55:51.500 协议[1965:139749] integer=10 2015-08-12 19:55:51.500 协议[1965:139749] Integer's initialization succeed. Program ended with exit code: 0
程序猿神奇的手,每时每刻,这双手都在改变着世界的交互方式!
本文转自当天真遇到现实博客园博客,原文链接:http://www.cnblogs.com/XYQ-208910/p/4725413.html,如需转载请自行联系原作者