浅析Objective-C中的Protocol协议

来源:互联网 发布:淘宝电商培训有哪些 编辑:程序博客网 时间:2024/05/21 21:02

在写java的时候都会有接口interface这个概念,接口就是一堆方法的声明没有实现,而在OC里面,Interface是一个

类的头文件的声明,并不是真正意义上的接口的意思,在OC中,接口是由一个叫做协议的protocol来实现的。这个里面

可以声明一些方法,和java不同的是,它可以声明一些必须实现的方法和选择实现的方法。这个和java是完全不同的。

下面我们就用一个例子来说明这个吧。

首先是MyProtocol.h也就是接口的声明文件

[cpp] view plaincopyprint?
  1. //  
  2. //  MyProtocol.h  
  3. //  Protocol  
  4. //  
  5. //  Created by bird on 12-10-22.  
  6. //  Copyright (c) 2012年 bird. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10.   
  11. @protocol MyProtocol <NSObject>  
  12.   
  13. @optional  
  14. //这个方法是可选的  
  15. - (void) print:(int) value;  
  16.   
  17. @required  
  18. //这个方法是必须实现的  
  19. - (int) printValue:(int) value1 andValue:(int) value2;  
  20.   
  21. @end  

然后是Mytest.h也就是类的声明文件,这个类讲实现这个接口

[cpp] view plaincopyprint?
  1. //  
  2. //  MyTest.h  
  3. //  Protocol  
  4. //  
  5. //  Created by bird on 12-10-22.  
  6. //  Copyright (c) 2012年 bird. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10. #import "MyProtocol.h"  
  11.   
  12. @interface MyTest : NSObject <MyProtocol>  
  13.   
  14. - (void) showInfo;  
  15.   
  16. @end  

下面就是实现文件了

[cpp] view plaincopyprint?
  1. //  
  2. //  MyTest.m  
  3. //  Protocol  
  4. //  
  5. //  Created by bird on 12-10-22.  
  6. //  Copyright (c) 2012年 bird. All rights reserved.  
  7. //  
  8.   
  9. #import "MyTest.h"  
  10.   
  11. @implementation MyTest  
  12.   
  13. - (void) showInfo  
  14. {  
  15.     NSLog(@"show info is calling");  
  16. }  
  17.   
  18. - (int) printValue:(int)value1 andValue:(int)value2  
  19. {  
  20.     NSLog(@"print info that value1 is %d and value2 is %d",value1,value2);  
  21.     return 0;  
  22. }  
  23.   
  24. //下面的这个方法可以实现也可以不实现  
  25. - (void) print:(int)value  
  26. {  
  27.     NSLog(@"print is value is %d",value);  
  28. }  
  29.   
  30. @end  

这样我们就可以看出了,这个类实现了接口也就是协议的声明的方法,然后还定义了自己的类方法。


下面我们来看一下主函数来使用这个接口,我将分别使用两种方式来使用这个类,一个是基本的方法就是使用类的创

建来调用,另一个就是使用接口来调用

[cpp] view plaincopyprint?
  1. //  
  2. //  main.m  
  3. //  Protocol  
  4. //  
  5. //  Created by bird on 12-10-18.  
  6. //  Copyright (c) 2012年 bird. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10. #import "MyTest.h"  
  11. #import "MyProtocol.h"  
  12.   
  13. int main(int argc, const char * argv[])  
  14. {  
  15.   
  16.     @autoreleasepool {  
  17.           
  18.         //这里是普通的类的调用方法  
  19.         MyTest * myTest = [[MyTest alloc] init];  
  20.         [myTest showInfo];  
  21.           
  22.         SEL sel = @selector(print:);//这个print转换成的方法  
  23.         //确定这个print方法是否实现  
  24.         if([myTest respondsToSelector:sel]){  
  25.                 [myTest print:20];  
  26.         }  
  27.           
  28.           
  29.         [myTest printValue:25 andValue:15];  
  30.         [myTest release];  
  31.           
  32.         //下面的方法使用协议的方式来调用  
  33.         id<MyProtocol> myProtocol = [[MyTest alloc] init];  
  34.         if([myProtocol respondsToSelector:@selector(print:)]){  
  35.             [myProtocol print:210];  
  36.         }  
  37.           
  38.         [myProtocol release];  
  39.     }  
  40.     return 0;  
  41. }  

[cpp] view plaincopyprint?
  1. [myProtocol respondsToSelector:@selector(print:)]  
这句话的用处就是测试是否这个类实现了这个方法
0 0
原创粉丝点击