[置顶] Objective-C,/,ios,/iphone开发基础:分类(category,又称类别)
在c++中我们可以多继承来实现代码复用和封装使程序更加简练。在objective-c中只能单继承,不能多继承,那么除了协议protocol之外,我们可以实现类似多继承的一个方法就是,分类(category,又称类别)。类别可以不修改原来的类(父类),和派生类的情况下,为原有的类增加新的方法,但是分类不能增加实例变量。
格式(format):
@interface class_name(category_name)<protocol,....>
method _declaration;
....
@end
@implementation class_name(category_name)
method_implementation;
...
@end
新建一个AddressCard类,然后再新建一个文件 AddressCardCategory 用来声明和实现分类(category)


1 #import <Foundation/Foundation.h>
2
3 @interface AddressCard : NSObject<NSCoding>{
4 NSString* name;
5 NSString* email;
6 }
7 @property (nonatomic,retain)NSString* name;
8 @property (nonatomic,retain)NSString* email;
9 -(id)initWithName:(NSString*)_name andEmail:(NSString*)_email;
10
11 @end




1 #import "AddressCard.h"
2
3 @implementation AddressCard
4 @synthesize name,email;
5 -(id)initWithName:(NSString*)_name andEmail:(NSString*)_email{
6 if(self= [super init])
7 {
8 self.name=_name;
9 self.email=_email;
10 }
11 return self;
12 }
13 -(void)encodeWithCoder:(NSCoder *)aCoder{
14 [aCoder encodeObject:name forKey:@"_name"];
15 [aCoder encodeObject:email forKey:@"_email"];
16 }
17
18 -(id)initWithCoder:(NSCoder*)aDecoder{
19 if(self=[super init])
20 {
21 self.name=[aDecoder decodeObjectForKey:@"_name"];
22 self.email=[aDecoder decodeObjectForKey:@"_email"];
23 }
24 return self;
25 }
26 -(void)dealloc{
27 [name release];
28 [email release];
29 [super dealloc];
30 }
31 @end




#import <Foundation/Foundation.h>
#import "AddressCard.h"
@interface AddressCard(category)
-(void)uppercaseName; @end




1 #import "AddressCardCategory.h"
2
3 @implementation AddressCard(Category)
4 -(void)uppercaseName{
5 self.name= [name uppercaseString];
6 }
7
8 @end




1 #import <Foundation/Foundation.h>
2 #import "AddressCardCategory.h"
3 int main (int argc, const char * argv[])
4 {
5
6 @autoreleasepool {
7 AddressCard* card1=[[AddressCard alloc]initWithName:@"shou" andEmail:@"abc@126.com"];
8 [NSKeyedArchiver archiveRootObject:card1 toFile:@"/tmp/AddressCard.txt"];
9 AddressCard* card4=[NSKeyedUnarchiver unarchiveObjectWithFile:@"/tmp/AddressCard.txt"];
10 NSLog(@"card 4 %@ ,%@",card4.name,card4.email);
11 [card4 uppercaseName];
12 NSLog(@"card 4 %@ ,%@",card4.name,card4.email);
13 [card1 release];
14 //[card4 release];
15
16
17 }
18 return 0;
19 }


执行结果:
2013-08-20 17:12:04.751 AddressCard[1079:707] card 4 shou ,abc@126.com
2013-08-20 17:12:04.758 AddressCard[1079:707] card 4 SHOU ,abc@126.com

[置顶] Objective-C,/,ios,/iphone开发基础:分类(category,又称类别)的更多相关文章
- [置顶] 提高生产力:Web开发基础平台WebCommon的设计和实现
Web开发中,存在着各种各样的重复性的工作.为了提高开发效率,不在当码农,我在思考和实践如何搭建一个Web开发的基础平台. Web开发基础平台的目标和功能 1.提供一套基础的开发环境,整合了常用的框架 ...
- [置顶] Objective-C ,ios,iphone开发基础:UIAlertView使用详解
UIAlertView使用详解 Ios中为我们提供了一个用来弹出提示框的类 UIAlertView,他类似于javascript中的alert 和c#中的MessageBox(); UIAlertVi ...
- [置顶] Objective-C ,ios,iphone开发基础:protocol 协议(委托,代理)的声明
协议是为了弥补Objective-c中类只能单继承的缺陷,在Objective-c2.0之前当一个类遵循一个协议的时候,必须在类中实现协议的所有方法,在Objective-c2.0之后协议中的方法就有 ...
- [置顶] Objective-C ,ios,iphone开发基础:在UITextField输入完以后,隐藏键盘,
在x-code Version 4.3.2 (4E2002)下编译: 在 Controller. m 文件下添加如下实例方法即可: - (void)viewDidUnload { [super vie ...
- [置顶] Objective-C ,ios,iphone开发基础:自定义控件:Eg: UIButton
第一步:新建一个工程,在 .h文件中坐如下声明: #import <UIKit/UIKit.h> @interface MyButtonViewController : UIViewCon ...
- [置顶] Objective-C ,/,ios,/iphone开发基础:协议(protocol)
protocol协议时为了补充Objective-C 只能单继承的缺陷而增加的一个新功能.Objective-C重所有的方法都是虚方法,所以在oc重也就没有关键字 virtual一说,有了协议可以补充 ...
- [置顶] Objective-C ,ios,iphone开发基础:ios数据库(The SQLite Database),使用终端进行简单的数据库操作
SQLite 是一个轻量级的免费关系数据库.SQLite最初的设计目标是用于嵌入式系统,它占用资源非常少,在嵌入式设备中,只需要几百K的内存就够了,可以在(http://www.sqlite.org ...
- [置顶] Objective-C ,ios,iphone开发基础:命名规范
命名规范:http://bukkake.iteye.com/blog/695492 点击打开链接
- Objective-C ,ios,iphone开发基础:使用GDataXML解析XML文档,(libxml/tree.h not found 错误解决方案)
使用GDataXML解析XML文档 在IOS平台上进行XML文档的解析有很多种方法,在SDK里面有自带的解析方法,但是大多情况下都倾向于用第三方的库,原因是解析效率更高.使用上更方便 这里主要介绍一下 ...
随机推荐
- EDMX更新实体后出现键值映射问题
近期做项目的EF改版时,在DB(ORACLE)中的表里添加一个新的PK,去除原有的PK. 在DB已添加完成操作,但这时在EDMX里进行从DB更新到EF里,更新完成后就发生如下错误提示: Error 6 ...
- Solr配置与简单Demo
简介: solr是基于Lucene Java搜索库的企业级全文搜索引擎,目前是apache的一个项目.它的官方网址在http://lucene.apache.org/solr/ .solr需要运行在 ...
- OC-手动内存管理
一.为什么要进行内存管理 •移动设备的内存极其有限,每个app所能占用的内存是有限制的 • •下列行为都会增加一个app的内存占用 Ø创建一个OC对象 Ø定义一个变量 Ø调用一个函数或者方法 • •当 ...
- call与apply的区别
/** * 用 add 来替换 sub,add.call(sub,3,1) == add(3,1) ,所以运行结果为:alert(4); */ function add(a,b){ alert(a+b ...
- Android SDK镜像
北京化工大学镜像站 http://ubuntu.buct.edu.cn/ 大连东软信息学院 http://mirrors.neusoft.edu.cn/ 中科院开源软件协会 http://mirror ...
- Qt标准对话框之QColorDialog
Qt中提供了一些标准的对话框,用于实现一些常用的预定义功能,比如本节中将要介绍的颜色对话框——QColorDialog. 在不同的系统平台下,颜色对话框的显示效果可能会有所不同,主要因系统主题风格而异 ...
- [转载]5分钟了解Mockito
原文链接: http://liuzhijun.iteye.com/blog/1512780/ 5分钟了解Mockito 博客分类: Open SourceJava 一.什么是mock测试,什么是moc ...
- jquery验证网址格式
在input中输入网址,用jquery验证输入网址是否正确 <input type="text" name="input-web" class=" ...
- What is Windows Clustering
A cluster is a group of independent computer systems, referred to as nodes, working together as a un ...
- Objective-C中的runtime的原理和用法
一.runtime介绍 runtime翻译就是运行时,我们称为运行时机制.在OC中最重要的体现就是消息发送机制. 1)在C语言中,程序在编译过程中就决定调用哪个函数. 2)在OC中,编译的时候不会决定 ...