OC中的协议

 你希望确保某个类实现一组确定的方法或者属性

协议是一个声明某些方法及属性并储存在实体文档。(通常延伸档名是.h)

任何实践协议的对象,都必须实践协议􏰀供的方法及属性(可在协议中指定是必须或可选)。

协议就像是一些规范,实践协议的类必须遵守这些规范。 

例如:

#import <Foundation/Foundation.h>
@protocol PersonProtocol <NSObject>
@optional
@property (nonatomic, strong) NSString *firstName;
@property (nonatomic, strong) NSString *lastName;
@property (nonatomic, unsafe_unretained) NSUInteger age;
@required
- (void) breathe;
@end 
#import <Foundation/Foundation.h>
#import "PersonProtocol.h"
@interface Father : NSObject <PersonProtocol> 
- (void) breathe;
@end

#import "Father.h"
@implementation Father
- (void) breathe{
/* Implement this method here */ }
@end

 

posted @ 2014-10-08 09:17  safiri  阅读(147)  评论(0编辑  收藏  举报