IOS学习笔记十三(使用类别实现非正式协议)
【摘要】 1、协议
可以理解为java里面的接口
2、测试Demo
NSObject+chenyu.h
#ifndef NSObject_chenyu_h#define NSObject_chenyu_h#import <Foundation/Foundation.h> @interface NSObj...
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 <Foundation/Foundation.h>
#import "NSObject+chenyu.h"
@interface Org : NSObject
@end
#endif /* Org_h */
Org.m
#import <Foundation/Foundation.h>
#import "Org.h"
//#import "NSObject+chenyu.h"
@implementation Org
//如果不实现协议中的方法会发生错误
-(void)test
{
NSLog(@"this is org test method");
}
@end
main.m
#import "Org.h"
int main(int argc, char * argv[]) {
@autoreleasepool {
Org *org = [Org new];
[org test];
}
}
3、运行结果
this is org test method
文章来源: chenyu.blog.csdn.net,作者:chen.yu,版权归原作者所有,如需转载,请联系作者。
原文链接:chenyu.blog.csdn.net/article/details/80960883
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)