【OC06】类目、延展、协议、计时器(连载八)
2015-08-06 08:30
531 查看
一、类目(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;
@end
Car.m文件中:
#import"Car.h"
//声明延展
@interfaceCar()
//延展中的方法是私有的
- (void)customExen;
@end
@implementationCar
- (void)run{
NSLog(@"car--------run");
}
- (void)speed;{
NSLog(@"car------speed");
}
//延展方法的实现(在类的实现中)
- (void)customExen{
NSLog(@"customExen------延展");
}
@end
//类目
@implementationCar (Other)
- (void)customRun{
NSLog(@"customRun-----类目");
}
//把原始类中的方法复写了,对象在调用此方法时,不会调用原始类的方法,而是会调用类目中的方法
- (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;
}
三、协议(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:
是否重复
[NSTimerscheduledTimerWithTimeInterval:1
target:ps
selector:@selector(test:)
userInfo:@"qwerr"
repeats:YES];
//停止计时器
// [timer invalidate];
//想要使用计时器,必须使用以下代码:
//让程序一直运行下去
[[NSRunLoopcurrentRunLoop]run];
//让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 ++;
}
@end
【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;
@end
Car.m文件中:
#import"Car.h"
//声明延展
@interfaceCar()
//延展中的方法是私有的
- (void)customExen;
@end
@implementationCar
- (void)run{
NSLog(@"car--------run");
}
- (void)speed;{
NSLog(@"car------speed");
}
//延展方法的实现(在类的实现中)
- (void)customExen{
NSLog(@"customExen------延展");
}
@end
//类目
@implementationCar (Other)
- (void)customRun{
NSLog(@"customRun-----类目");
}
//把原始类中的方法复写了,对象在调用此方法时,不会调用原始类的方法,而是会调用类目中的方法
- (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;
}
三、协议(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:
是否重复
[NSTimerscheduledTimerWithTimeInterval:1
target:ps
selector:@selector(test:)
userInfo:@"qwerr"
repeats:YES];
//停止计时器
// [timer invalidate];
//想要使用计时器,必须使用以下代码:
//让程序一直运行下去
[[NSRunLoopcurrentRunLoop]run];
//让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 ++;
}
@end
相关文章推荐
- 如何选择路由协议
- BGP边界网关协议
- AS3脚本编写的计时器效果代码
- Node.js中使用计时器定时执行函数详解
- sliderToggle在写jquery的计时器setTimeouter中不生效
- JavaScript表达式:URL 协议介绍
- Android短信操作常见协议和常用代码
- JS 页面计时器示例代码
- 路由信息协议RIP
- 路由器原理及路由协议
- Android Wear计时器开发
- php中获取主机名、协议及IP地址的方法
- javascript实现的简单计时器
- JavaScript计时器示例分析
- javascript计时器详解
- android开发教程之间隔执行程序(android计时器)
- python实现简单的计时器功能函数
- Python远程桌面协议RDPY安装使用介绍
- android之计时器(Chronometer)的使用以及常用的方法