标签:imei number 特性 target import pre proc 程序 scheduled
在代码的头文件里,假如有例如以下这么多的成员变量,也就须要对应的 setter, 和 getter 方法
#import <Foundation/Foundation.h>
@interface Things : NSObject
{
NSUInteger thing1;
NSUInteger thing2;
NSUInteger thing3;
NSUInteger thing4;
NSUInteger thing5;
}
- (void)resetAllValue;
- (void)print;
- (void)setThing1:(NSUInteger)thing1;
- (NSUInteger)thing1;
- (void)setThing2:(NSUInteger)thing2;
- (NSUInteger)thing2;
- (void)setThing3:(NSUInteger)thing3;
- (NSUInteger)thing3;
- (void)setThing4:(NSUInteger)thing4;
- (NSUInteger)thing4;
- (void)setThing5:(NSUInteger)thing5;
- (NSUInteger)thing5;
@end
这样对应的. m 文件就会繁琐到不行,这才是5个变量,假如是100个,须要很多人来完毕,总不能让每一个人都拿着核心文件吧,于是把它们分散开很有必要,而类别给我们提供了一种这种机制,仅仅须要一个总的实现文件 .m,然后把当中部分方法分散到其类别文件里去实现.例如以下, 头文件Things.h文件要改动代码例如以下:
@interface Things : NSObject
{
NSUInteger thing1;
NSUInteger thing2;
NSUInteger thing3;
NSUInteger thing4;
NSUInteger thing5;
}
- (void)resetAllValue;
- (void)print;
//类的扩展例如以下
@interface Things (thing1)
- (void)setThing1:(NSUInteger)thing1;
- (NSUInteger)thing1;
@end
@interface Things (thing2)
- (void)setThing2:(NSUInteger)thing2;
- (NSUInteger)thing2;
@end
@interface Things (thing3)
- (void)setThing3:(NSUInteger)thing3;
- (NSUInteger)thing3;
@end
@interface Things (thing4)
- (void)setThing4:(NSUInteger)thing4;
- (NSUInteger)thing4;
@end
@interface Things (thing5)
- (void)setThing5:(NSUInteger)thing5;
- (NSUInteger)thing5;
@end
实现文件 Things.m 的内容仅仅须要核心的方法,其余让其扩展类别去做:
#import "Things.h"
@implementation Things
- (void)print
{
NSLog(@"%ld,%ld,%ld,%ld,%ld",self.thing1,self.thing2,self.thing3,self.thing4,self.thing5);
}
- (void)resetAllValue
{
self.thing1 = 100;
self.thing2 = 200;
}
@end
其余详细实现,即须要好多人做的部分,给他们分开,以下举一个 Things+Thing1.m 文件
#import "Things.h"
@implementation Things (Thing1)
- (void)setThing1:(NSUInteger)t
{
thing1 = t;
}
- (NSUInteger)thing1
{
return thing1;
}
@end
@selector(setEngine:)
@selector(setTire:atIndex)
QYCar *car = [[Car alloc] init];
if([car respondsToSelector:@selector(setEngine:)]){
NSLog(@"hihi");
}
以下,我就用一个简单的样例来实现非正式协议实现托付
第一步,创建名 QYXiaoMing的类继承自NSObject 类,在创建一个 QYXiaoHong 的类,来实现一个这种功能:
小明学习累了,想让小红在他睡着后某段时间后再把他叫起来,这时候小红叫小明这个动作就是一个托付 .详细代码例如以下:
QYXiaoMing.h 的内容
#import <Foundation/Foundation.h>
@interface QYXiaoMing : NSObject
//小明学习累了,有学习的行为
- (void)startSleep:(NSUInteger) time;
@end//QYXiaoMing.h
QYXiaoMing.m 的内容
#import "QYXiaoMing.h"
#import "QYXiaoHong.h"
@implementation QYXiaoMing
- (void)startSleep:(NSUInteger)time
{
NSLog(@"Sleep...");
QYXiaoHong *xiaohong = [[QYXiaoHong alloc] init];
[xiaohong call:time];
}
@end//QYXiaoMing.m
头文件QYXiaoHong.h
#import <Foundation/Foundation.h>
@interface QYXiaoHong : NSObject
- (void)call:(NSUInteger)time;
@end//QYXiaoHong.h
详细实现QYXiaoHong.m
#import "QYXiaoHong.h"
@implementation QYXiaoHong
- (void)call:(NSUInteger)time
{
//须要在指定的时间里把小明叫起来(计时器的使用方法)
[NSTimer scheduledTimerWithTimeInterval:time target:self selector:@selector(callXiaoMing) userInfo:nil repeats:NO];
}
@end //QYXiaoHong.m
类别头文件NSObject+CallXiaoMing.h
#import <Foundation/Foundation.h>
@interface NSObject (CallXiaoMing)
- (void)callXiaoMing;
@end
类别实现文件NSObject+CallXiaoMing.m
#import "NSObject+CallXiaoMing.h"
@implementation NSObject (CallXiaoMing)
- (void)callXiaoMing
{
NSLog(@"lalala,don‘t sleep!");
}
@end
main.m 的代码
#import <Foundation/Foundation.h>
#import "QYXiaoMing.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
QYXiaoMing *xiaoming = [[QYXiaoMing alloc] init];
[xiaoming startSleep:5];//5是5秒
//要求程序运行到这里,不能结束, 是为了让显示的时间循环显示下
[[NSRunLoop currentRunLoop] run];
}
return 0;
}//main.m
标签:imei number 特性 target import pre proc 程序 scheduled
原文地址:http://www.cnblogs.com/lxjshuju/p/7244268.html