OC基础之Category,Extension,Protocol
来源:互联网 发布:知不足者好学的下一句 编辑:程序博客网 时间:2024/06/05 14:41
类别:Category
说明,很多时间我只想在一个类上扩展我需要的功能,这个时候,如果只是继承这个类,可能有点大题小作,这个时候我们就引入了类别的概念,类别是OC中特有的
我们创建的时候选择OC-file类型去创建.
注意:类别不能在扩展任何成员变量。
1.声明类别中需要扩展的方法
// MyPoint+ZL.h
// OC//// Created by liuking on 16/1/31.// Copyright © 2016年 liuking. All rights reserved.//#import "MyPoint.h"@interface MyPoint (ZL)-(void) allSize;@end
//// MyPoint+ZL.m// OC//// Created by liuking on 16/1/31.// Copyright © 2016年 liuking. All rights reserved.//#import "MyPoint+ZL.h"@implementation MyPoint (ZL)-(void) allSize{ NSLog(@"我是类别扩展出来的信息");}@end
调用刚刚实现的类别
//// main.m// OC//// Created by liuking on 16/1/31.// Copyright © 2016年 liuking. All rights reserved.//#import <Foundation/Foundation.h>#import "Bar.h"#import "Foo.h"#import "MyPoint.h"#import "PointObserver.h"#import "MyPoint+ZL.h"int main(int argc, const char * argv[]) { @autoreleasepool { MyPoint *point = [[MyPoint alloc] init]; [point allSize];
有时有些方法只想自己看见,而不公开,我们可以使用延展,然后在实现类里实现这个方法
下面这个isMyPoint方法就需要在MyPoint中实现,否则会报警
//// MyPoint_Extend.h// OC//// Created by liuking on 16/1/31.// Copyright © 2016年 liuking. All rights reserved.//#import "MyPoint.h"@interface MyPoint () //() 一般不指定名字-(void) isMyPoint;@end
需要实现延展里面的方法
//// MyPoint.m// OC//// Created by liuking on 16/1/31.// Copyright © 2016年 liuking. All rights reserved.//#import "MyPoint.h"#import "MyPoint_Extend.h"@implementation MyPoint@synthesize x;@synthesize y;-(void)isMyPoint{ NSLog(@"我是延展出来的信息。。。。");}
协议:Protocol
java里面有个接口interface的概念,接口就是一堆方法只是声明,还没有实现。而在OC里面由一个叫协议的protocol来声明一些方法,和java不一同的是,OC可以声音必须现实与选择实现的方法。这和java是不同的
1.使用协议可以使用两个关键字:第一个是@required关键字,该关键字后面的方法必须 实现,如果没有指定@required关键字,协议中所有的方法都是默认必须实现的。
2.协议不能有成员变量
定义一个Protocol
//// IProtocol.h// OC//// Created by liuking on 16/1/31.// Copyright © 2016年 liuking. All rights reserved.//#import <Foundation/Foundation.h>@protocol IProtocol <NSObject>@optional //这可是选择实现的方法-(void) read;@required //这个是必须实现的方法-(void) write;@end
使用协议
//// ProtocolTest.h// OC//// Created by liuking on 16/1/31.// Copyright © 2016年 liuking. All rights reserved.//#import <Foundation/Foundation.h>#import "IProtocol.h"@interface ProtocolTest : NSObject<IProtocol>@end//// ProtocolTest.m// OC//// Created by liuking on 16/1/31.// Copyright © 2016年 liuking. All rights reserved.//#import "ProtocolTest.h"@implementation ProtocolTest-(void)write //这个是必须实现的方法{ NSLog(@"wtrie some things");}-(void) read //选择实现的方法{ NSLog(@"read some things");}@end
int main(int argc, const char * argv[]) { @autoreleasepool { ProtocolTest *test = [[ProtocolTest alloc] init]; if ([test respondsToSelector:@selector(read)]) { /<span style="color:#ff0000;">/注意这里一定要判断是否有实现read方法,否则会闪退</span> [test read]; } [test write];
声明一个必须实现协议的对象,通常需要使用id数据类型,还需要在关键字id后面指定对象要遵守的协议
声明一个实现某协议的变量
id<IProtocol> *delegate;
0 0
- OC基础之Category,Extension,Protocol
- 黑马程序员----OC学习笔记之category,extension,protocol
- OC 类目category,延展extension,协议protocol
- OC第七天 --- NSDate Category Extension Protocol
- Object基础语法Category、Extension、Protocol
- ios之Category,Extension,Protocol,delegate
- OC中的类别Category-协议Protocol-扩展Extension
- OC 类目(category) 延展(extension) 协议(protocol)
- 随意细解:OC -- NSData、Category、Extension、Protocol
- Category、Extension、Protocol
- Category Extension protocol 总结
- 8、category、extension、protocol
- Category、Protocol、Extension
- OC之Cagegory, Extension, Protocol协议
- Objective C基础(四)---Category、Extension、Protocol
- 进阶之路(基础篇)OC——Category&Extension
- 类目 Category Extension 协议 Protocol
- Category , Extension , Protocol几个概念
- Ubuntu12.04安装中文输入法
- 快慢指针------Linked List Cycle
- 用SlidingMenu实现仿QQ侧滑+Viewpage+ListView多布局
- web应用程序的目录结构
- Android学习-ListView+ CheckBox实现单选
- OC基础之Category,Extension,Protocol
- 如何将tomcat服务器启动端口改为80
- 自动重置事件练习
- iOS应用提交到APP Store错误码ERROR ITMS-90071
- JAVA web设置默认首页
- HDOJ 2037 今年暑假不AC
- 求三个数的最小公倍数
- 你不再能自称是小白
- OC基础之对象归档与读取文件