一、类目(Category)
【1】为什么使用类目:
当一个类不能被修改时,如何扩展它的新功能。
(1)通过定义一个他的子类,来实现扩展功能。
(2)使用类目,为现有类扩展新的方法,扩展的方法会成为原始类的一部分。
通过类目扩展的方法,子类也能继承下来。
【2】类目的定义
(1)类目的定义与类的定义相似。.h文件声明,.m文件实现。
(2)命名规则:类名+类目名 eg:”NSString+Extension”。
【3】类目的局限性:
(1)类目可以复写现有类的方法。复写后,原始方法无法调用。类目中的方法调用优先级比原始类中的要高。
(2)类目不能为类扩展实例属性(即:类目中不能使用实例变量)
二、延展
在类的实现文件.m中定义类目,叫延展。
//******延展、类目的使用 例子**********************
Car.h文件中:
#import<Foundation/Foundation.h>
@interfaceCar : NSObject
{
int a;
}
- (void)run;
- (void)speed;
@end
//类目
@interfaceCar (Other)
//类目中不允许定义实例变量
//{
// int b;
//}
- (void)customRun;
@interfaceCar : NSObject
{
int a;
}
- (void)run;
- (void)speed;
@end
//类目
@interfaceCar (Other)
//类目中不允许定义实例变量
//{
// int b;
//}
- (void)customRun;
@end
Car.m文件中:
#import"Car.h"
//声明延展
@interfaceCar()
//延展中的方法是私有的
- (void)customExen;
@end
@interfaceCar()
//延展中的方法是私有的
- (void)customExen;
@end
@implementationCar
- (void)run{
NSLog(@"car--------run");
}
- (void)speed;{
NSLog(@"car------speed");
- (void)run{
NSLog(@"car--------run");
}
- (void)speed;{
NSLog(@"car------speed");
}
//延展方法的实现(在类的实现中)
- (void)customExen{
NSLog(@"customExen------延展");
}
@end
- (void)customExen{
NSLog(@"customExen------延展");
}
@end
//类目
@implementationCar (Other)
- (void)customRun{
NSLog(@"customRun-----类目");
@implementationCar (Other)
- (void)customRun{
NSLog(@"customRun-----类目");
}
//把原始类中的方法复写了,对象在调用此方法时,不会调用原始类的方法,而是会调用类目中的方法
- (void)speed{
//调用自己内部的私有方法(延展中)
[selfcustomExen];
NSLog(@"speed--------类目");
}
- (void)speed{
//调用自己内部的私有方法(延展中)
[selfcustomExen];
NSLog(@"speed--------类目");
}
@end
main.m文件中:
#import<Foundation/Foundation.h>
#import "Car.h"
intmain(intargc, const char * argv[]) {
@autoreleasepool {
//****** 延展、类目的使用 **********************
Car *car = [[Caralloc]init];
//调用原始类中的方法
[car run];
//调用类目中的方法
[car customRun];
//调用类目中的方法
[car speed];
}
return 0;
#import "Car.h"
intmain(intargc, const char * argv[]) {
@autoreleasepool {
//****** 延展、类目的使用 **********************
Car *car = [[Caralloc]init];
//调用原始类中的方法
[car run];
//调用类目中的方法
[car customRun];
//调用类目中的方法
[car speed];
}
return 0;
}
三、协议(Protocol)
(1)协议是定义一组方法,让其他类去实现
(2)协议本身不是类
(3)协议文件只有.h文件
【1】协议的定义
@required:表示必须实现的方法,默认的
@optional:表示可选的实现方法
【2】实现协议
eg:若学生类需要实现协议,则需在其.h文件中导入协议文件,并在其父类名后加上“<协议名>”。并在其.m文件中,实现协议中要求的方法。
eg:
#import"XieYi.h"
@interfaceStudent :
NSObject<XieYi>
@end
四、计时器的使用
main.m文件中:
//*******计时器的使用******************
Person *ps = [[Personalloc]init];
//定时器的使用
//NSTimeInterval: 时间的间隔
//target: 目标(谁使用)
//selector: 目标调用的方法
//repeats: 是否重复
//NSTimeInterval: 时间的间隔
//target: 目标(谁使用)
//selector: 目标调用的方法
//repeats: 是否重复
[NSTimerscheduledTimerWithTimeInterval:1
target:ps
selector:@selector(test:)
userInfo:@"qwerr"
repeats:YES];
//停止计时器
// [timer invalidate];
//想要使用计时器,必须使用以下代码:
//让程序一直运行下去
[[NSRunLoopcurrentRunLoop]run];
//让runloop一直运行到某一个date
NSDate *date = [NSDatedateWithTimeIntervalSinceNow:10];
[[NSRunLoopcurrentRunLoop]runUntilDate:date];
//让runloop一直运行到某一个date
NSDate *date = [NSDatedateWithTimeIntervalSinceNow:10];
[[NSRunLoopcurrentRunLoop]runUntilDate:date];
Person.m文件中:
@implementationPerson
intcount = 0;
- (void)test:(NSTimer*)timer
{
if (count== 10) {
[timer invalidate];
return;
}
NSLog(@"person --- test, timer : %@", timer.userInfo);
count ++;
}
intcount = 0;
- (void)test:(NSTimer*)timer
{
if (count== 10) {
[timer invalidate];
return;
}
NSLog(@"person --- test, timer : %@", timer.userInfo);
count ++;
}
@end