OC中的Category类别

来源:互联网 发布:淘宝怎么防止别人盗图 编辑:程序博客网 时间:2024/04/28 12:11
类别是一种为现有的类添加新方法的方式。

 @interface NSString (NumberConvenience)
- (NSNumber *)lengthAsNumber;
@end
(1)为NSString类添加一个名称为NumberConveniencede的类别;类别名称具有唯一性,你可以向一个类中添加任意多的类别。
(2)可以指定希望向其添加类别的类(NSString),以及类别的名称(NumberConvenience),而且你还可以列出添加的方法,最后以@end结束;类别声明部分不能有实例变量部分
实现类别
@implementation NSString (NumberConvenience)
- (NSNmuber *)lengthAsNumber{
       unsigned int length = [self length];//获得字符串长度
       return ([NSNumber numberWithUnsignedInt :length]);

@end
#import <Foundation/Foundation.h>
#import "CategoryThing.h"
//类别的作用:
//(1)将类别实现分散到多个不同文件或多个不同框架中
//(2)创建私有方法的前向引用
//(3)向对象添加非正式协议
//类别的局限性:
//(1)无法添加新的实例变量
//(2)名称冲突,如果类别和现有的方法重名,类别具有更高的优先级,解决办法,类别方法名中添加一个前缀
@interface NSString (NumberConvenience)
- (NSNumber *) lengthAsNumber;
@end
@implementation NSString (NumberConvenience)
- (NSNumber *) lengthAsNumber
{
unsigned int length= [self length];
return ([NSNumber numberWithUnsignedInt:length]);
}
@end
int main (int argc, c*****t char * argv[]) {
//我们适用类别创建的所有NSNumber类的对象将在自动释放池中被销毁一样,可变字典也将在这里被销毁
NSAutoreleasePool * pool = [[NSAutoreleasePool allocinit];
// insert code here...

NSMutableDictionary *dict;
dict=[NSMutableDictionary dictionary];

//使用键@“hello”将整值5添加到字典中的代码如下
[dict setObject:[@"hello" lengthAsNumberforKey@"hello"];

[dict setObject:[@"iLikeFish" lengthAsNumberforKey@"iLikeFish"];

[dict setObject:[@"Once upon a time" lengthAsNumberforKey@"Once upon a time"];

    NSLog(@"%@",dict);

CategoryThing *thing;
thing= [[CategoryThing allocinit];

[thing setThing1:5];
[thing setThing2:23];
[thing setThing3:42];

NSLog(@"Thing are %@!",thing);

[thing release];

    [pool drain];
    return 0;
}
//
//  CategoryThing.h
//  S12_leibie
//
//  Created by cwity on 11-5-17.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import <Cocoa/Cocoa.h>
@interface CategoryThing : NSObject {
int thing1;
int thing2;
int thing3;
}
@end
@interface CategoryThing (Thing1)
- (void) setThing1:(intthing1;
- (int) thing1;
@end
@interface CategoryThing (Thing2)
- (void) setThing2:(intthing2;
- (int) thing2;
@end
@interface CategoryThing (Thing3)
- (void) setThing3:(intthing3;
- (int) thing3;
@end
//
//  CategoryThing.m
//  S12_leibie
//
//  Created by cwity on 11-5-17.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "CategoryThing.h"
@implementation CategoryThing
- (NSString *) description
{
NSString *desc;
desc= [NSString stringWithFormat:@"%d %d %d",
    thing1,thing2,thing3];

return (desc);
}
@end
//
//  Thing1.m
//  S12_leibie
//
//  Created by cwity on 11-5-17.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "CategoryThing.h"
@implementation CategoryThing (Thing1)
- (void) setThing1:(int)t1
{
thing1=t1;
}
- (int) thing1
{
return (thing1);
}
@end
//
//  Thing2.m
//  S12_leibie
//
//  Created by cwity on 11-5-17.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "CategoryThing.h"
@implementation CategoryThing (Thing2)
- (void) setThing2:(int)t2
{
thing2=t2;
}
- (int) thing2
{
return (thing2);
}
//
//  Thing3.m
//  S12_leibie
//
//  Created by cwity on 11-5-17.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import"CategoryThing.h"
@implementation CategoryThing (Thing3)
- (void) setThing3:(int)t3
{
thing3=t3;
}
- (int) thing3
{
return(thing3);
}
@end


总结:
什么时候使用类别?

(1)类别只能添加新方法,无法添加新的实例变量。

(2)如果类别名和原来类中的方法产生名称冲突,则类别将覆盖原来的方法,因为类别具有更高的优先级。

要注意的是Objective-c只支持单继承,如果要实现多继承的话,可以通过类别和协议的方式来实现。

另外要特别注意的是,类别不能像继承时那样给类别接口增加新的实例变量,而是要扩展一个类的行为。
类别的名称是任意的。
0 0
原创粉丝点击