当前位置: 首页 > news >正文

Objective-C:在类中设置不同协议

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

//我设置的协议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

 

转载于:https://www.cnblogs.com/XYQ-208910/p/4725413.html

相关文章:

  • React Native 简介:用 JavaScript 搭建 iOS 应用(2)
  • 以ASPX生成静态页
  • android获得屏幕高度和宽度
  • 项目直播:任务管理系统应用
  • 苹果电脑键盘符号记录
  • 转:Windows 8上强制Visual Studio以管理员身份运行
  • BZOJ 1047: [HAOI2007]理想的正方形( 单调队列 )
  • HDU 2955(0-1背包问题)
  • Unity Shader:Projective Texture Mapping
  • POJ-3414 Pots (BFS)
  • 台大机器学习基石课程之机器学习基本原理和概念
  • 【转载】Android 开发 命名规范
  • 九、UINavigationController切换视图 实例
  • MySQL学习笔记-数据库文件
  • I2C总线的仲裁机制
  • 自己简单写的 事件订阅机制
  • 【RocksDB】TransactionDB源码分析
  • 【笔记】你不知道的JS读书笔记——Promise
  • 【许晓笛】 EOS 智能合约案例解析(3)
  • JavaWeb(学习笔记二)
  • Js实现点击查看全文(类似今日头条、知乎日报效果)
  • React中的“虫洞”——Context
  • SQL 难点解决:记录的引用
  • Zepto.js源码学习之二
  • 前端技术周刊 2019-01-14:客户端存储
  • 前端设计模式
  • 容器服务kubernetes弹性伸缩高级用法
  • 如何编写一个可升级的智能合约
  • 如何正确配置 Ubuntu 14.04 服务器?
  • 为物联网而生:高性能时间序列数据库HiTSDB商业化首发!
  • 温故知新之javascript面向对象
  • media数据库操作,可以进行增删改查,实现回收站,隐私照片功能 SharedPreferences存储地址:
  • ​软考-高级-系统架构设计师教程(清华第2版)【第20章 系统架构设计师论文写作要点(P717~728)-思维导图】​
  • ​无人机石油管道巡检方案新亮点:灵活准确又高效
  • ​学习一下,什么是预包装食品?​
  • #{}和${}的区别?
  • $forceUpdate()函数
  • ( 10 )MySQL中的外键
  • (1综述)从零开始的嵌入式图像图像处理(PI+QT+OpenCV)实战演练
  • (C语言)深入理解指针2之野指针与传值与传址与assert断言
  • (day 2)JavaScript学习笔记(基础之变量、常量和注释)
  • (html5)在移动端input输入搜索项后 输入法下面为什么不想百度那样出现前往? 而我的出现的是换行...
  • (Java数据结构)ArrayList
  • (pt可视化)利用torch的make_grid进行张量可视化
  • (翻译)terry crowley: 写给程序员
  • (附源码)spring boot球鞋文化交流论坛 毕业设计 141436
  • (附源码)spring boot智能服药提醒app 毕业设计 102151
  • (算法)Travel Information Center
  • (学习日记)2024.03.25:UCOSIII第二十二节:系统启动流程详解
  • (原創) 如何將struct塞進vector? (C/C++) (STL)
  • (转载)深入super,看Python如何解决钻石继承难题
  • .NET Micro Framework初体验(二)
  • .NET文档生成工具ADB使用图文教程
  • [ 数据结构 - C++]红黑树RBTree
  • [.NET 即时通信SignalR] 认识SignalR (一)