OC中的Category类别
2015-07-07 19:49
483 查看
类别是一种为现有的类添加新方法的方式。
@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只支持单继承,如果要实现多继承的话,可以通过类别和协议的方式来实现。
另外要特别注意的是,类别不能像继承时那样给类别接口增加新的实例变量,而是要扩展一个类的行为。
类别的名称是任意的。
@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只支持单继承,如果要实现多继承的话,可以通过类别和协议的方式来实现。
另外要特别注意的是,类别不能像继承时那样给类别接口增加新的实例变量,而是要扩展一个类的行为。
类别的名称是任意的。
相关文章推荐
- K-Nearest Neighbors algorithm
- Algorithms—33.Search in Rotated Sorted Array
- 给分类(Category)添加属性
- 聊一聊google的Knowledge Graph
- view设置gone的时候getHeight()的值
- django + uwsgi 配置
- Django DEBUG = False 引起的问题 django - Setting DEBUG = False causes 500 Error
- Algorithms—14.Longest Common Prefix
- golang 文件简单操作
- Django运行方式及处理流程总结
- Algorithms—9.Palindrome Number
- Why we need template on Django ?
- 关于delegate, category和subclass
- 手动更新google服务框架
- 审美、效用与情感——关于icon和Logo设计的一些想法
- 算法基础:删除字符串中出现次数最少的字符(Golang实现)
- 从Check Point 官网使用Google Chrome 下载补丁需要特别注意的地方
- django inspectdb
- Django模块笔记【六】
- django 包含标签例子