博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Objective-C:在类中设置不同协议
阅读量:6983 次
发布时间:2019-06-27

本文共 3994 字,大约阅读时间需要 13 分钟。

在下面的代码中,设置了两种不同的协议规则:一种是老师对学生设置的协议:即老师发出命令后,学生站起来、回答问题、坐下; 另一种是我对学生设置的协议:即学生按照我的协议中的初始化函数去初始化一个整数。

//我设置的协议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 
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 
9 @protocol TeacherDelegate//老师制定协议规则10 @required //在协议代理人类中必须要全部实现的方法11 -(void) show1;12 -(void) Standup;13 -(void) Answerquestion;14 @optional //协议代理人类中可以选择性的实现(可以实现,也可以不用实现)15 -(void) Setdown;16 @end17 18 19 @interface Teacher : NSObject20 @property(nonatomic,weak) id
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 Teacher11 @synthesize delegate;12 -(void)ordering13 {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
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 Student11 @synthesize integer;12 //TeacherDelegate13 -(void) show114 {15     NSLog(@"the teacher's protocol is running!");16 }17 -(void) Standup18 {19     NSLog(@"student stand up.");20 }21 -(void) Answerquestion22 {23     NSLog(@"student answer question.");24 }25 -(void) Setdown26 {27     NSLog(@"student set down!");28 }29 //Myprotocol30 -(void) show231 {32     NSLog(@"the my protocol is running!");33 }34 -(id)initWithInteger:(NSInteger) i35 {36     self = [super init];37     if(self)38     {39         self.integer = i;40     }41     return self;42 }43 -(void)print44 {45     NSLog(@"integer=%ld",self.integer);46 }47 -(void) initialize48 {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 
9 #import "Teacher.h"10 #import "Student.h"11 #import "Myprotocol.h"12 int main(int argc, const char * argv[])13 {14 @autoreleasepool15 {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=102015-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,如需转载请自行联系原作者
你可能感兴趣的文章
c#时间转换
查看>>
调度器Quartz的简述与使用总结
查看>>
smokeping 安装
查看>>
Linux下安装oracle数据库步骤
查看>>
yum 不小心删除后安装
查看>>
vim 使用
查看>>
为敏感信息设置安全屏障
查看>>
mysql fabric安装使用测试
查看>>
java 对 mongoDB 分组统计操作 以及一些常用操作
查看>>
当你扛不住的时候就读读
查看>>
解决安装rrdtool遇到的一个问题
查看>>
linux启动过程
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
xmlUtil 解析 创建
查看>>
我的友情链接
查看>>
linux 命令(3)echo
查看>>
Nginx基础入门之nginx基础配置项介绍(2)
查看>>
一次详细全面的***报告
查看>>
c# 三种异步编程模型EAP(*)、 APM(*)和 TPL
查看>>