协议0

//ProtocolClass.h

#import <Foundation/Foundation.h>

//声明一个协议:@protocol+协议名+满足的基协议
@protocol MyselfDelegate <NSObject>

@required//修饰的方法为必须实现的方法
-(void)helloProtocolForR;

@optional//修饰的方法为可选实现的方法
-(void)helloProtocolForO;

@end

//<>表示该类遵守MyselfDelegate协议,则可以实现MyselfDelegate协议中的方法(.h无须再次声明)
@interface ProtocolClass : NSObject<MyselfDelegate>

@end

//ProtocolClass.m

#import "ProtocolClass.h"

@implementation ProtocolClass

//实现协议中的方法

-(void)helloProtocolForR
{
    NSLog(@"MyselfDelegate协议中必须实现的方法");
}

-(void)helloProtocolForO
{
    NSLog(@"MyselfDelegate协议中可选实现的方法");
}

@end

//AnotherProtocolClass.h另一个遵守

1 #import <Foundation/Foundation.h>
2 #import "ProtocolClass.h"
3 
4 @interface AnotherProtocolClass : NSObject<MyselfDelegate>
5 
6 @end

//AnotherProtocolClass.m

#import "AnotherProtocolClass.h"

@implementation AnotherProtocolClass

//实现协议中的方法

-(void)helloProtocolForR
{
    NSLog(@"MyselfDelegate协议中必须实现的方法");
}

-(void)helloProtocolForO
{
    NSLog(@"MyselfDelegate协议中可选实现的方法");
}

@end

  

 

posted @ 2015-11-10 19:03  开心刘哈哈  阅读(182)  评论(0编辑  收藏  举报