学过java的同学都知道Interface(接口),那么在Objective-C中有没有接口呢?其实 Objective-C中用Protocol(协议)来实现的,在Objective-C具体怎么用,我们直接看代码例子。

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团队】 原文链接: http://www.cnblogs.com/superdo/p/4575504.html

最新文章

  1. 机器学习实战笔记(Python实现)-04-Logistic回归
  2. 结合数据库登录注册模块,登录成功之后跳到WebView
  3. 解决JettyMavenPlugin: Failed to load class &quot;org.slf4j.impl.StaticLoggerBinder&quot;
  4. dr.wondr博士随笔之某古旧非智能机T6XXX 恢复一例
  5. python网络编程【二】(使用TCP)
  6. 如何激活webstorm 11
  7. CXF学习(2) helloworld
  8. socket编程与利用进程进行多并行连接
  9. iOS 第三方自定义Alertview项目MBProcessHud中的重要代码分析
  10. 【原创】Docker容器及Spring Boot微服务应用
  11. Git CMD - rm: Remove files from the working tree and from the index
  12. 关于Form窗体的StartPosition 属性如何设置的问题
  13. [Leetcode][Python]33: Search in Rotated Sorted Array
  14. 1455:An Easy Problem
  15. Mybatis源码之Statement处理器BaseStatementHandler(二)
  16. Linux进程状态解析之R、S、D、T、Z、X
  17. 了解一下RabbitMQ
  18. [转]Docker(三):Dockerfile 命令详解
  19. GMA Round 1 逃亡
  20. 暂时关闭 windows 病毒防护

热门文章

  1. RocketMQ-架构篇
  2. python安装pycrypto库
  3. 软件——IDEA中如何去掉警告虚线
  4. OpenCV 4下darknet修改
  5. 带你看看Java的锁(一)-ReentrantLock
  6. MySQL基础总结(二)
  7. PHP导出excel文件,第二步先实现自写二维数组加入模板excel文件后导出
  8. Pytest 单元测试框架
  9. [hdu5215]无向图找奇偶环
  10. 5-JVM常用的命令