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 alloc] init];
// insert code here...
NSMutableDictionary *dict;
dict=[NSMutableDictionary dictionary];
//使用键@“hello”将整值5添加到字典中的代码如下
[dict setObject:[@"hello" lengthAsNumber] forKey: @"hello"];
[dict setObject:[@"iLikeFish" lengthAsNumber] forKey: @"iLikeFish"];
[dict setObject:[@"Once upon a time" lengthAsNumber] forKey: @"Once upon a time"];
NSLog(@"%@",dict);
CategoryThing *thing;
thing= [[CategoryThing alloc] init];
[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:(int) thing1;
- (int) thing1;
@end
@interface CategoryThing (Thing2)
- (void) setThing2:(int) thing2;
- (int) thing2;
@end
@interface CategoryThing (Thing3)
- (void) setThing3:(int) thing3;
- (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
另外要特别注意的是,类别不能像继承时那样给类别接口增加新的实例变量,而是要扩展一个类的行为。
类别的名称是任意的。
@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 alloc] init];
// insert code here...
NSMutableDictionary *dict;
dict=[NSMutableDictionary dictionary];
//使用键@“hello”将整值5添加到字典中的代码如下
[dict setObject:[@"hello" lengthAsNumber] forKey: @"hello"];
[dict setObject:[@"iLikeFish" lengthAsNumber] forKey: @"iLikeFish"];
[dict setObject:[@"Once upon a time" lengthAsNumber] forKey: @"Once upon a time"];
NSLog(@"%@",dict);
CategoryThing *thing;
thing= [[CategoryThing alloc] init];
[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:(int) thing1;
- (int) thing1;
@end
@interface CategoryThing (Thing2)
- (void) setThing2:(int) thing2;
- (int) thing2;
@end
@interface CategoryThing (Thing3)
- (void) setThing3:(int) thing3;
- (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
- OC中的Category类别
- OC中的类别(category)
- OC中的类别Category-协议Protocol-…
- OC Category(类别)
- OC中的类别Category-协议Protocol-扩展Extension
- OC category类别的使用
- IOS OC 类别category浅析
- OC--category(类目、分类、类别)
- 【iOS沉思录】OC中的类扩展机制:Category类别和Extension类扩展
- oc中的类别
- OC中的类别解析
- oc中的类别
- OC中的分类Category
- Object-C中的动态特性(Category类别)
- IOS中的类别(Category)和扩展(Extension)
- Objective-C中的类别(Category)
- category 类别
- 类别category
- Matlab如何创建多个Legend(图例)
- The method of type serviceimpl must override a superclass method解决方式
- CodeForces 251A. Points on Line(数学 lower_bound )
- 猜猜看游戏开发总结
- javamail
- OC中的Category类别
- HDU 3065 病毒侵袭持续中 (AC自动机)
- 81.根据公式计算值
- Ubuntu下的抓包工具tcpdump
- XDebug用法大全(二)断点调试和性能分析
- Hadoop格式化HDFS报错java.net.UnknownHostException: localhost.localdomain: localhost.localdomain
- DWR配置时遇到的错误
- 使用IE8开发工具调试时,遇到“无法附加进程,进程可能附加了另一个调试程序”的解决方案
- TCP连接状态分析:SYNC_RECV,CLOSE_WAIT,TIME_WAIT