协议可以有多个直接父协议,协议只能继承协议,不能继承类。 协议中可以有类方法声明也可以有实例方法声明,但不能有实现。
例如
#import <Foundation/Foundation.h>
@protocol PA
-(void)paFunc;
@end
@protocol PB
-(void)pbFunc;
@end
@protocol PC <PA,PB>
-(void)pcFunc;
@end
@interface MyClass : NSObject <PC>
@end
@implementation MyClass
-(void)paFunc{
NSLog(@"pa");
}
-(void)pbFunc{
NSLog(@"pb");
}
-(void)pcFunc{
NSLog(@"pc");
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
MyClass*clz=[[MyClass alloc]init];
[clz paFunc];
[clz pbFunc];
[clz pcFunc];
NSObject <PC>* pc=[[MyClass alloc]init];
[pc paFunc];
[pc pbFunc];
[pc pcFunc];
}
return 0;
}
打印如下
2015-09-20 15:40:46.979 testt[732:113939] pa
2015-09-20 15:40:46.981 testt[732:113939] pb
2015-09-20 15:40:46.981 testt[732:113939] pc
2015-09-20 15:40:46.981 testt[732:113939] pa
2015-09-20 15:40:46.981 testt[732:113939] pb
2015-09-20 15:40:46.981 testt[732:113939] pc