[Objective-C] 006_Protocol(协议)
2015-06-14 18:45
453 查看
学过java的同学都知道Interface(接口),那么在Objective-C中有没有接口呢?其实 Objective-C中用Protocol(协议)来实现的,在Objective-C具体怎么用,我们直接看代码例子。
StudentProtocol
StudentProtocol 已经写好了那要怎么用呢,我们以Student 类为例,Student要实现StudentProtocol 只需在Student.h 中类名后加入<StudentProtocol>,Student.m 实现StudentProtocol中定义的方法即可。
在StudentProtocol 声明了两个方法,有一个可选实现,那我们有没有办法知道 Student 到底实现了这个方法呢?有的,继续看代码。
测试结果:
2015-06-14 18:23:13.104 Attendance[16464:617950] 我遵循了 --- StudentProtocol --- 协议
2015-06-14 18:23:13.104 Attendance[16464:617950] 我还是学生,谈不谈恋爱都可以...,但是我还是在和---> 如花 <---谈恋爱。
本站文章为 宝宝巴士 SD.Team 原创,转载务必在明显处注明:(作者官方网站: 宝宝巴士 )
转载自【宝宝巴士SuperDo团队】 原文链接: /article/6669078.html
StudentProtocol
//////////////////// .h ///////////////////// #import <Foundation/Foundation.h> @protocol StudentProtocol <NSObject> @optional //下面的方法是可选实现的 - (void)fallInLove:(NSString *)name; @required //下面的方法是必须实现的 - (void)curriculum; @end
StudentProtocol 已经写好了那要怎么用呢,我们以Student 类为例,Student要实现StudentProtocol 只需在Student.h 中类名后加入<StudentProtocol>,Student.m 实现StudentProtocol中定义的方法即可。
//////////////// .h /////////////////// #import "Person.h" #import "StudentProtocol.h" @interface Student : Person <StudentProtocol> - (id)initWithName:(NSString *)name sex:(NSString *)sex age:(int)age; @end //////////////// .m /////////////////// #import "Student.h" @implementation Student - (id)initWithName:(NSString *)name sex:(NSString *)sex age:(int)age { self = [super init]; if (self) { self.name = name; self.sex = sex; self.age = age; } return self; } - (Person *)work { NSLog(@"%@正在工作",self.name); return 0; } - (void)printInfo { NSLog(@"我的名字叫:%@ 今年%d岁 我是一名%@ %@",self.name,self.age,self.sex,NSStringFromClass([self class])); } #pragma mark StudentProtocol - (void)fallInLove:(NSString *)name { NSLog(@"我还是学生,谈不谈恋爱都可以...但是我还是在和 ---> %@ <--- 谈恋爱",name); } - (void)curriculum { NSLog(@"我是学生,必须上课学习..."); } @end
在StudentProtocol 声明了两个方法,有一个可选实现,那我们有没有办法知道 Student 到底实现了这个方法呢?有的,继续看代码。
#import "AppDelegate.h" #import "Student.h" #import "StudentProtocol.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { Student *s = [[Student alloc] initWithName:@"小明" sex:@"男" age:12]; if ([s conformsToProtocol:@protocol(StudentProtocol)]) { //判断是否遵循了某个协议 NSLog(@"我遵循了 --- StudentProtocol --- 协议"); if ([s respondsToSelector:@selector(fallInLove:)]) { //判断是否有实现某个方法 [s fallInLove:@"如花"]; } } return YES; } @end
测试结果:
2015-06-14 18:23:13.104 Attendance[16464:617950] 我遵循了 --- StudentProtocol --- 协议
2015-06-14 18:23:13.104 Attendance[16464:617950] 我还是学生,谈不谈恋爱都可以...,但是我还是在和---> 如花 <---谈恋爱。
本站文章为 宝宝巴士 SD.Team 原创,转载务必在明显处注明:(作者官方网站: 宝宝巴士 )
转载自【宝宝巴士SuperDo团队】 原文链接: /article/6669078.html
相关文章推荐
- Object-c-数组的使用
- 关于等待多长时间会引发ORA-04021: timeout occurred while waiting to lock object错误的猜测
- objective-c中线程编程一例
- objective-c中线程编程一例
- objective-c中线程编程一例
- Object-C中需要注意的小细节
- 使用Object#tap使代码更优雅
- 学习笔记(objective-c)-重写description方法
- WebSocket使用javax.websocket.RemoteEndpoint.Basic.sendObject(Object arg0)向页面方法发送对象
- java笔记10 多态 Object类
- UGUI - 判断是否点击在UI 上 Bug,IsPointerOverGameObject()在移动端检测失败
- ObjectOutStream的reset方法
- 学习笔记(objective-c)-判断指针变量的实际类型
- Python标准库:内置函数vars([object])
- Python标准库:内置函数vars([object])
- 学习笔记(objective-c)-多态
- 【ObjectC】oc中的内存分配
- error: while loading shared libraries : countlib.so : cannnot open shared object file : No such fil
- Faq_flask : AttributeError: ‘module’ object has no attribute ‘autoescape’
- Objective-C语法之扩展(Extension)的使用