浅析Objective-C中的Protocol协议
2015-07-23 17:04
597 查看
在写java的时候都会有接口interface这个概念,接口就是一堆方法的声明没有实现,而在OC里面,Interface是一个
类的头文件的声明,并不是真正意义上的接口的意思,在OC中,接口是由一个叫做协议的protocol来实现的。这个里面
可以声明一些方法,和java不同的是,它可以声明一些必须实现的方法和选择实现的方法。这个和java是完全不同的。
下面我们就用一个例子来说明这个吧。
首先是MyProtocol.h也就是接口的声明文件
[cpp] view
plaincopyprint?
//
// MyProtocol.h
// Protocol
//
// Created by bird on 12-10-22.
// Copyright (c) 2012年 bird. All rights reserved.
//
#import <Foundation/Foundation.h>
@protocol MyProtocol <NSObject>
@optional
//这个方法是可选的
- (void) print:(int) value;
@required
//这个方法是必须实现的
- (int) printValue:(int) value1 andValue:(int) value2;
@end
然后是Mytest.h也就是类的声明文件,这个类讲实现这个接口
[cpp] view
plaincopyprint?
//
// MyTest.h
// Protocol
//
// Created by bird on 12-10-22.
// Copyright (c) 2012年 bird. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "MyProtocol.h"
@interface MyTest : NSObject <MyProtocol>
- (void) showInfo;
@end
下面就是实现文件了
[cpp] view
plaincopyprint?
//
// MyTest.m
// Protocol
//
// Created by bird on 12-10-22.
// Copyright (c) 2012年 bird. All rights reserved.
//
#import "MyTest.h"
@implementation MyTest
- (void) showInfo
{
NSLog(@"show info is calling");
}
- (int) printValue:(int)value1 andValue:(int)value2
{
NSLog(@"print info that value1 is %d and value2 is %d",value1,value2);
return 0;
}
//下面的这个方法可以实现也可以不实现
- (void) print:(int)value
{
NSLog(@"print is value is %d",value);
}
@end
这样我们就可以看出了,这个类实现了接口也就是协议的声明的方法,然后还定义了自己的类方法。
下面我们来看一下主函数来使用这个接口,我将分别使用两种方式来使用这个类,一个是基本的方法就是使用类的创
建来调用,另一个就是使用接口来调用
[cpp] view
plaincopyprint?
//
// main.m
// Protocol
//
// Created by bird on 12-10-18.
// Copyright (c) 2012年 bird. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "MyTest.h"
#import "MyProtocol.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
//这里是普通的类的调用方法
MyTest * myTest = [[MyTest alloc] init];
[myTest showInfo];
SEL sel = @selector(print:);//这个print转换成的方法
//确定这个print方法是否实现
if([myTest respondsToSelector:sel]){
[myTest print:20];
}
[myTest printValue:25 andValue:15];
[myTest release];
//下面的方法使用协议的方式来调用
id<MyProtocol> myProtocol = [[MyTest alloc] init];
if([myProtocol respondsToSelector:@selector(print:)]){
[myProtocol print:210];
}
[myProtocol release];
}
return 0;
}
[cpp] view
plaincopyprint?
[myProtocol respondsToSelector:@selector(print:)]
这句话的用处就是测试是否这个类实现了这个方法
类的头文件的声明,并不是真正意义上的接口的意思,在OC中,接口是由一个叫做协议的protocol来实现的。这个里面
可以声明一些方法,和java不同的是,它可以声明一些必须实现的方法和选择实现的方法。这个和java是完全不同的。
下面我们就用一个例子来说明这个吧。
首先是MyProtocol.h也就是接口的声明文件
[cpp] view
plaincopyprint?
//
// MyProtocol.h
// Protocol
//
// Created by bird on 12-10-22.
// Copyright (c) 2012年 bird. All rights reserved.
//
#import <Foundation/Foundation.h>
@protocol MyProtocol <NSObject>
@optional
//这个方法是可选的
- (void) print:(int) value;
@required
//这个方法是必须实现的
- (int) printValue:(int) value1 andValue:(int) value2;
@end
然后是Mytest.h也就是类的声明文件,这个类讲实现这个接口
[cpp] view
plaincopyprint?
//
// MyTest.h
// Protocol
//
// Created by bird on 12-10-22.
// Copyright (c) 2012年 bird. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "MyProtocol.h"
@interface MyTest : NSObject <MyProtocol>
- (void) showInfo;
@end
下面就是实现文件了
[cpp] view
plaincopyprint?
//
// MyTest.m
// Protocol
//
// Created by bird on 12-10-22.
// Copyright (c) 2012年 bird. All rights reserved.
//
#import "MyTest.h"
@implementation MyTest
- (void) showInfo
{
NSLog(@"show info is calling");
}
- (int) printValue:(int)value1 andValue:(int)value2
{
NSLog(@"print info that value1 is %d and value2 is %d",value1,value2);
return 0;
}
//下面的这个方法可以实现也可以不实现
- (void) print:(int)value
{
NSLog(@"print is value is %d",value);
}
@end
这样我们就可以看出了,这个类实现了接口也就是协议的声明的方法,然后还定义了自己的类方法。
下面我们来看一下主函数来使用这个接口,我将分别使用两种方式来使用这个类,一个是基本的方法就是使用类的创
建来调用,另一个就是使用接口来调用
[cpp] view
plaincopyprint?
//
// main.m
// Protocol
//
// Created by bird on 12-10-18.
// Copyright (c) 2012年 bird. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "MyTest.h"
#import "MyProtocol.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
//这里是普通的类的调用方法
MyTest * myTest = [[MyTest alloc] init];
[myTest showInfo];
SEL sel = @selector(print:);//这个print转换成的方法
//确定这个print方法是否实现
if([myTest respondsToSelector:sel]){
[myTest print:20];
}
[myTest printValue:25 andValue:15];
[myTest release];
//下面的方法使用协议的方式来调用
id<MyProtocol> myProtocol = [[MyTest alloc] init];
if([myProtocol respondsToSelector:@selector(print:)]){
[myProtocol print:210];
}
[myProtocol release];
}
return 0;
}
[cpp] view
plaincopyprint?
[myProtocol respondsToSelector:@selector(print:)]
这句话的用处就是测试是否这个类实现了这个方法
相关文章推荐
- Git fatal: failed to read object ..... : Invalid argument
- [Object-C] AutoLaout入门
- Objective-C学习笔记(十三)——函数的声明与调用
- [Objective-C]__bridge,__bridge_retained和__bridge_transfer的意思,区别与使用
- QObject怎样把ppt嵌入qt widget里显示,并控制自动播放
- Object类的重要方法
- Django Related objects
- Objective-C学习笔记(十二)——switch多分支语句的使用
- TaskQueS.c---分配工作并以WaitForSingleObject()等待之
- iOS Object-C 基础 UIlabel
- Objective-C学习笔记(十一)——break和continue的使用方法
- Error creating object Please verify that the Microsoft Data Access Components 2.1 (or later) have be
- Objective-C学习笔记(十)——循环语句for和do-while的使用
- Objective-C学习笔记(九)——循环语句while的使用方法
- Objective-C学习之旅 第三篇
- 无法确定 <T>T 的类型参数 ;对于上限为int,java.lang.Object 的类型变量 T,不存在唯一最大实例
- Objective-C学习笔记(八)——高级跳转语句goto使用方法
- Objective-C中的@property
- oc中alloc和new的区别
- Objective-C学习笔记(七)——if-else条件判断语句