[置顶] 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里面有自带的解析方法,但是大多情况下都倾向于用第三方的库,原因是解析效率更高.使用上更方便 这里主要介绍一下 ...
随机推荐
- Unity3D 之UGUI 按钮
新建一个按钮 按钮对应的属性 按钮下面只有一个文本的 游戏对象,是Button对应的显示文字 Interactable -->是否可以交互 Transition -->变换,对应各种关于按 ...
- Css3动画库收集
1.animate.css – 齐全的CSS3动画库 http://www.dowebok.com/98.html 2.Angular官方动画库 http://augus.github.io/ngAn ...
- Unity3D用vistual studio打卡C#脚本卡死解决
小黑已经跟我3年了,不仅很喜欢他方正酷黑的外表,而且稳定性绝对没的说.我已经3年没有重装过系统了,而且现在装了3个系统!虽然小黑很适合程序员,但是他最大的缺点就是做设计比较吃力,显卡512M.像uni ...
- windows下能读写linux分区的软件 转
1. ext2ifs 这个工具与explore2fs都是John Newbigin使用Delphi写的,explore2fs Copyright (C) 2000,Ext2IFS v0.3 Copyr ...
- iOS控件——UIView与UIImageView播放动画的实现方法
1.UIView //初始状态 [UIView animateWithDuration:(int) animations:^{ //最终状态 }completion:^(BOOL finished){ ...
- OpenCV学习(1)-安装(Windows)
下载安装 在这里下载.我下载了2.4.9的Windows版本.双击安装即可. 配置环境变量 配置环境变量的目的是为了让系统找到OpenCV的动态链接库.因此需要把动态链接库添加到系统环境变量PATH中 ...
- 关于C++对汉字拼音的处理(3)
之所以汉字转拼音的博文能出到3,主要是因为没有很完美的C++的解决方案,但是写到了这里可以有一个小结了. 以前的方法都有这种那种弊端,如果出现了无法识别的汉字(简体的)就无法修改处理了,但是下面的这种 ...
- 九度OJ 1371 最小的K个数 -- 堆排序
题目地址:http://ac.jobdu.com/problem.php?pid=1371 题目描述: 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4 ...
- groovy --不注意的小错误(java.lang.String.positive() is applicable)
sql 语句拼接报错: No signature of method: java.lang.String.positive() is applicable for argument types: () ...
- git ignore已经checked in files
对于untracked file, 可以使用.gitignore 对于已经checked in file,可以使用git update-index #隐藏 git update-index --ass ...