OC中的Category类别

类别是一种为现有的类添加新方法的方式。

 @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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值