(1)、分类的基础知识编程
分类是在不改变原有类内容的基础上,为类增长一些方法的方式。闭包
(2)、分类的注意事项框架
1>分类只能增长方法,不能增长成员变量;编程语言
2>在分类方法的实现中能够访问原来类中的成员变量;函数
3>分类中能够从新实现原来类中的方法,可是这样会致使原来的方法没法在使用ui
4>在大规模应用中。一般把相应的功能写成分类,对原有的类进行扩充,通常分模块写,一个模块一个分类this
咱们能够再上一节Student类的基础上增长分类:atom
CateStudent.h方法声明:spa
1 #import <Foundation/Foundation.h> 2 #import "Student.h" 3 @interface Student (CatStudent) 4 - (void)eat; 5 - (void)study; 6 @end
CateStudent.m方法实现:code
1 #import "CateStudent.h" 2 @implementation Student (CatStudent) 3 4 - (void)eat { 5 NSLog(@"add eat,age=%i...",age); 6 } 7 8 - (void)study { 9 NSLog(@"add study..."); 10 } 11 12 @end
main.m的实现:
1 #import <Foundation/Foundation.h> 2 #import "CateStudent.h" 3 int main(int argc, const char * argv[]) 4 { 5 6 @autoreleasepool { 7 8 // insert code here... 9 Student *stu = [[Student alloc]init]; 10 [stu eat]; 11 [stu study]; 12 [stu release]; 13 } 14 return 0; 15 }
(1)、协议介绍
在ObjC中使用@protocol定义一组方法规范,实现此协议的类必须实现对应的方法。这就像面向对象编程语言中的接口同样,由于OC中Interface已经用于定义类了,因此定义接口用了一个新的关键字Protocol。
(2)、协议的注意事项
1>一个协议能够扩展自另外一个协议,若是须要扩展多个协议中间使用逗号分隔;
2>和其余高级语言中接口不一样的是协议中定义的方法不必定是必须实现的,咱们能够经过关键字进行@required和@optional进行设置,若是不设置则默认是@required(注意ObjC是弱语法,即便不实现必选方法编译运行也不会报错);
3>协议经过<>进行实现,一个类能够同时实现多个协议,中间经过逗号分隔;
4>协议的实现只能在类的声明上,不能放到类的实现上(也就是说必须写成@interface Person:NSObject<AnimalDelegate>而不能写成@implementation Person<AnimalDelegate>);
5>协议中不能定义属性、成员变量等,只能定义方法;
3、协议的实现
协议的声明PersonDelegate.h文件:
1 #import <Foundation/Foundation.h> 2 3 @protocol PersonDelegate <NSObject> 4 @required 5 - (void) eat; 6 7 @optional 8 9 - (void)run; 10 - (void)say; 11 @end
Student.h文件:
1 #import <Foundation/Foundation.h> 2 #import "PersonDelegate.h" 3 @interface Student : NSObject<PersonDelegate> 4 5 - (void) eat; 6 - (void) study; 7 8 @end
Student.m文件:
1 #import "Student.h" 2 3 @implementation Student 4 - (void) eat { 5 NSLog(@"eat..."); 6 } 7 - (void) study { 8 NSLog(@"study..."); 9 } 10 @end
在不少语言中的事件机制都是经过回调函数来实现的,由于框架不能提早知道在发生某个事件的时候用户如何处理这个事件,因此在框架要提供一个能让用户注册本身的事件处理函数的功能。在ObjC中也有相似的方法,称之为代码块(Block)。Block就是一个函数体(匿名函数),它是OC对于闭包的实现,在块状中咱们能够持有或引用局部变量,同时利用Block也能够将一个函数做为一个参数进行传递。
MyButton.h
1 #import <Foundation/Foundation.h> 2 @class MyButton; 3 typedef void(^clickEvent)(MyButton*); 4 5 @interface MyButton : NSObject 6 7 @property(nonatomic,copy)clickEvent click; 8 -(void)onClick; 9 @end
MyButton.m
1 #import "MyButton.h" 2 3 @implementation MyButton 4 5 -(void)onClick{ 6 NSLog(@"on click it..."); 7 if(self.click){ 8 _click(self); 9 } 10 } 11 @end
main.m
1 #import <Foundation/Foundation.h> 2 #import "MyButton.h" 3 int main(int argc, const char * argv[]) 4 { 5 6 @autoreleasepool { 7 MyButton *btn=[[MyButton alloc]init]; 8 btn.click=^(MyButton *b){ 9 NSLog(@"this is click event..."); 10 }; 11 [btn onClick]; 12 } 13 return 0; 14 }
关于Block须要注意一下几点: