天天看点

IOS学习笔记十三(使用类别实现非正式协议)

1、协议

可以理解为java里面的接口

2、测试Demo

NSObject+chenyu.h

#ifndef NSObject_chenyu_h

#define NSObject_chenyu_h

#import <Foundation/Foundation.h>

@interface NSObject(chenyu)

-(void)test;

@end

#endif /* NSObject_chenyu_h */

Org.h

#ifndef Org_h

#define Org_h

#import "NSObject+chenyu.h"

@interface Org : NSObject

#endif /* Org_h */

Org.m

#import "Org.h"

//#import "NSObject+chenyu.h"

@implementation Org

//如果不实现协议中的方法会发生错误

-(void)test

{

   NSLog(@"this is org test method");

}

main.m

int main(int argc, char * argv[]) {

   @autoreleasepool {

       Org *org = [Org new];

       [org test];

   }

3、运行结果

this is org test method