Objective-C /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)
#import <Foundation/Foundation.h>
@interface AddressCard : NSObject<NSCoding>{
NSString* name;
NSString* email;
}
@property (nonatomic,retain)NSString* name;
@property (nonatomic,retain)NSString* email;
-(id)initWithName:(NSString*)_name andEmail:(NSString*)_email;
@end
AddressCard.h
#import "AddressCard.h" @implementation AddressCard
@synthesize name,email;
-(id)initWithName:(NSString*)_name andEmail:(NSString*)_email{
if(self= [super init])
{
self.name=_name;
self.email=_email;
}
return self;
}
-(void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:name forKey:@"_name"];
[aCoder encodeObject:email forKey:@"_email"];
} -(id)initWithCoder:(NSCoder*)aDecoder{
if(self=[super init])
{
self.name=[aDecoder decodeObjectForKey:@"_name"];
self.email=[aDecoder decodeObjectForKey:@"_email"];
}
return self;
}
-(void)dealloc{
[name release];
[email release];
[super dealloc];
}
@end
AddressCard.m
#import <Foundation/Foundation.h>
#import "AddressCard.h"
@interface AddressCard(category)
-(void)uppercaseName; @end
AddressCard(category)分类声明文件
#import "AddressCardCategory.h" @implementation AddressCard(Category)
-(void)uppercaseName{
self.name= [name uppercaseString];
} @end
AddressCard(Category)实现文件
#import <Foundation/Foundation.h>
#import "AddressCardCategory.h"
int main (int argc, const char * argv[])
{ @autoreleasepool {
AddressCard* card1=[[AddressCard alloc]initWithName:@"shou" andEmail:@"abc@126.com"];
[NSKeyedArchiver archiveRootObject:card1 toFile:@"/tmp/AddressCard.txt"];
AddressCard* card4=[NSKeyedUnarchiver unarchiveObjectWithFile:@"/tmp/AddressCard.txt"];
NSLog(@"card 4 %@ ,%@",card4.name,card4.email);
[card4 uppercaseName];
NSLog(@"card 4 %@ ,%@",card4.name,card4.email);
[card1 release];
//[card4 release]; }
return ;
}
main 函数如下:
执行结果:
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 /iphone开发基础:分类(category,又称类别)的更多相关文章
- [置顶] Objective-C,/,ios,/iphone开发基础:分类(category,又称类别)
在c++中我们可以多继承来实现代码复用和封装使程序更加简练.在objective-c中只能单继承,不能多继承,那么除了协议protocol之外,我们可以实现类似多继承的一个方法就是,分类(catego ...
- 从C#到Objective-C,循序渐进学习苹果开发(3)--分类(category)和协议Protocal的理解
本随笔系列主要介绍从一个Windows平台从事C#开发到Mac平台苹果开发的一系列感想和体验历程,本系列文章是在起步阶段逐步积累的,希望带给大家更好,更真实的转换历程体验.本文继续上一篇随笔<从 ...
- 深入理解iPhone数据持久化(手把手教你iphone开发 – 基础篇)
在所有的移动开发平台数据持久化都是很重要的部分:在j2me中是rms或保存在应用程序的目录中,在symbian中可以保存在相应的磁盘目录中和数据库中.symbian中因为权限认证的原因,在3rd上大多 ...
- OC基础--分类(category) 和 协议(protocol)
OC 中的category分类文件相当于 C#中的部分类:OC 中的protocol协议文件(本质是头文件)相当于 C#中的接口.今天就简单说明一下OC中的这两个文件. 由于视频中的Xcode版本低, ...
- Objective-C /iphone开发基础:协议(protocol)
protocol协议时为了补充Objective-C 只能单继承的缺陷而增加的一个新功能.Objective-C重所有的方法都是虚方法,所以在oc重也就没有关键字 virtual一说,有了协议可以补充 ...
- [置顶] Objective-C ,/,ios,/iphone开发基础:协议(protocol)
protocol协议时为了补充Objective-C 只能单继承的缺陷而增加的一个新功能.Objective-C重所有的方法都是虚方法,所以在oc重也就没有关键字 virtual一说,有了协议可以补充 ...
- iPhone开发基础教程_第二章
1.各个子文件夹的作用 Classes: 编写的大多代码都保存在这里,其中包括所有的Objective-C类,可以在Classes文件夹下创建一些子 ...
- Objective-C ,ios,iphone开发基础:几个常用类-NSNumber
2013-08-21 在Objective-C,包括int double float 等等再内的基础数据类型都不是一个类,所以就不能给它们发送消息,也就是说不能调用方法,那怎么办呢 ?Objectiv ...
- Objective-C ,ios,iphone开发基础:快速实现一个简单的图片查看器
新建一个single view 工程: 关闭ARC , 在.xib视图文件上拖放一个UIImageView 两个UIButton ,一个UISlider ,布局如图. 并为他们连线, UIImage ...
随机推荐
- HDU 3567 Eight II BFS预处理
题意:就是八数码问题,给你开始的串和结束的串,问你从开始到结束的最短且最小的变换序列是什么 分析:我们可以预处理打表,这里的这个题可以和HDU1430魔板那个题采取一样的做法 预处理打表,因为八数码问 ...
- Python脚本控制的WebDriver 常用操作 <二十四> 定位frame中的元素
测试用例场景 处理frame需要用到2个方法,分别是switch_to_frame(name_or_id_or_frame_element)和switch_to_default_content() 如 ...
- java 基础之数据类型
java 数据类型这个地方面试的时候会被经常问到,很多人并不注意这个问题,今天带大家全面了解一下.java数据类型主要分:1.基本数据类型 2.引用数据类型 3.空类型 下面一一介绍. 基本数据类型包 ...
- opencv linux
This link which you also mentioned describes the necessary steps to compile OpenCV on your machine. ...
- 七牛上传Qt版本
最近在找图床,写博客啥的需要.以前的图床好像挂了,搭在BAE上的图床也挂了,可能BAE3.0更新了吧. 花了点时间写了Qt版本 github地址:https://github.com/wzyuliya ...
- 线性存储结构-LinkedList
LinkedList内部采用链表的形式构建,是一个双向链表.除了继承List外,还继承了Deque接口,可以当做堆栈结构使用. private static final class Link<E ...
- 转:Unicode字符集和多字节字符集关系
原文地址: http://my.oschina.net/alphajay/blog/5691 unicode.ucs-2.ucs-4.utf-16.utf-32.utf-8 http://stallm ...
- Bzoj-2005 能量采集 gcd,递推
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2005 题意:题目转换后的模型就是求Σ(gcd(x,y)), 1<=x<=n, ...
- HDU-4607 Park Visit bfs | DP | dfs
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4607 首先考虑找一条最长链长度k,如果m<=k+1,那么答案就是m.如果m>k+1,那么最 ...
- A Tour of Go Exercise: HTTP Handlers
Implement the following types and define ServeHTTP methods on them. Register them to handle specific ...