OC的分类和协议
2015-01-09 23:22
197 查看
分类和协议是OC中比较显著的俩个特点。分类的功能主要是实现类的扩展,协议则常常用在代理的实现上。
1、分类的声明
在分类的接口文件中,只允许新增方法,不能新增变量。语法形式:
@interface 类名 (分类名)
新增的方法声明;
@end
2、定义方法
在分类的实现文件中,对新增方法进行定义,语法如下:
@implementation 类名 (分类名)
新增的方法的定义{
语句;
}
@end
3、方法调用
分类的方法声明和定义之后,就可以进行调用了,语法如下:
[对象名 新增方法名]
实例:
#import <Foundation/Foundation.h>
@interface student : NSObject{
int val;
}
@end
#import "student.h"
@interface student (pupil)
-(int)intval;
@end
#import "student+pupil.h"
@implementation student (pupil)
-(int)intval{
val=200;
return val;
}
@end
#import <Foundation/Foundation.h>
#import "student.h"
#import "student+pupil.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
student *stu=[[student alloc] init];
NSLog(@"%i",[stu intval]);
}
return 0;
}
输出结果:200
协议实际上是一组方法列表,它不依赖于特定的类。使用协议可以使不同的类共享相同的消息。
1、协议的定义
一个协议的定义是放在头文件中的,其语法如下:
@protocol 协议名
(@optional或@required)方法声明;
@end
一般在方法声明前面,有2个关键字进行修饰:@optional,表示声明的方法是可选的;@required表示声明的方法是必须的。默认是必须实现的。
2、协议在类中的声明
要在类中使用协议,首先要在类中进行协议的声明,语法如下:
@interface 类名:父类名 <协议名>
@end
3、实现
协议实现语法如下:
@implementation 类名 (分类名)
类和协议中的声明方法的实现{
语句;
}
@end
实例:
#import <Foundation/Foundation.h>
@protocol protocalcc <NSObject>
-(void)go;
@end
#import <Foundation/Foundation.h>
#import "protocalcc.h"
@interface student : NSObject<protocalcc>
@end
#import "student.h"
@implementation student
-(void)go{
NSLog(@"go student");
}
@end
#import <Foundation/Foundation.h>
#import "student.h"
#import "student+pupil.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
student *stu=[[student alloc] init];
[stu go];
}
return 0;
}
输出结果:go student
在此程序中用到了协议继承,即协议2使用了协议1,语法如下:
@protocol 协议名2 <协议名1>
(@optional或@required)方法声明;
@end
在OC中,默认为创建的协议都是在协议中使用协议,协议名1为NSObject,语法如下:
@protocol 协议名 <NSObject>
(@optional或@required)方法声明;
@end
具有多个协议的使用
在大多数情况下,一个类中是可以使用多个协议的,@interface 类名:父类名<协议1,协议2,...协议n>
1、分类的声明
在分类的接口文件中,只允许新增方法,不能新增变量。语法形式:
@interface 类名 (分类名)
新增的方法声明;
@end
2、定义方法
在分类的实现文件中,对新增方法进行定义,语法如下:
@implementation 类名 (分类名)
新增的方法的定义{
语句;
}
@end
3、方法调用
分类的方法声明和定义之后,就可以进行调用了,语法如下:
[对象名 新增方法名]
实例:
#import <Foundation/Foundation.h>
@interface student : NSObject{
int val;
}
@end
#import "student.h"
@interface student (pupil)
-(int)intval;
@end
#import "student+pupil.h"
@implementation student (pupil)
-(int)intval{
val=200;
return val;
}
@end
#import <Foundation/Foundation.h>
#import "student.h"
#import "student+pupil.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
student *stu=[[student alloc] init];
NSLog(@"%i",[stu intval]);
}
return 0;
}
输出结果:200
协议实际上是一组方法列表,它不依赖于特定的类。使用协议可以使不同的类共享相同的消息。
1、协议的定义
一个协议的定义是放在头文件中的,其语法如下:
@protocol 协议名
(@optional或@required)方法声明;
@end
一般在方法声明前面,有2个关键字进行修饰:@optional,表示声明的方法是可选的;@required表示声明的方法是必须的。默认是必须实现的。
2、协议在类中的声明
要在类中使用协议,首先要在类中进行协议的声明,语法如下:
@interface 类名:父类名 <协议名>
@end
3、实现
协议实现语法如下:
@implementation 类名 (分类名)
类和协议中的声明方法的实现{
语句;
}
@end
实例:
#import <Foundation/Foundation.h>
@protocol protocalcc <NSObject>
-(void)go;
@end
#import <Foundation/Foundation.h>
#import "protocalcc.h"
@interface student : NSObject<protocalcc>
@end
#import "student.h"
@implementation student
-(void)go{
NSLog(@"go student");
}
@end
#import <Foundation/Foundation.h>
#import "student.h"
#import "student+pupil.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
student *stu=[[student alloc] init];
[stu go];
}
return 0;
}
输出结果:go student
在此程序中用到了协议继承,即协议2使用了协议1,语法如下:
@protocol 协议名2 <协议名1>
(@optional或@required)方法声明;
@end
在OC中,默认为创建的协议都是在协议中使用协议,协议名1为NSObject,语法如下:
@protocol 协议名 <NSObject>
(@optional或@required)方法声明;
@end
具有多个协议的使用
在大多数情况下,一个类中是可以使用多个协议的,@interface 类名:父类名<协议1,协议2,...协议n>
相关文章推荐
- OC中分类、类扩展、Block、协议(由协议引出的代理模式)
- oc 分类与协议
- OC学习7——@class、Category分类、protocol协议
- OC_协议与分类的学习
- OC分类和协议
- OC 分类 协议
- 【黑马程序员-学习笔记】OC-协议与分类
- oc协议,延展,分类
- 黑马程序员------OC中协议和分类
- 黑马程序员——ios开发基础之OC中的分类、协议、延展和代理
- oc中的分类/协议/属性
- 黑马程序员---OC学习笔记之分类(Category)非正式协议【重点】
- 黑马程序员——OC基础---分类(Category)、非正式协议及分类延展
- (九)黑马程序员——oc基础之分类与协议
- OC中的分类和协议
- OC_ 分类和协议
- 黑马程序员IOS-OC语言-分类、协议
- 03-OC中的分类(Category),代码块(Block),协议(property)
- oc中分类 扩展 协议的使用及区别
- OC基础8:分类和协议