Objective-C基调(4)Category
OC它提供了一种不同的方式--Category,可以动态地添加新的行为已经存在的类(方法),这确保了较小的类的原始设计,然后逐渐加入扩展。
正在使用Category扩张的上课时间,你并不需要创建一个子类,Category使用简单的方法。实现类模块化的相关方法,类方法分配到不同的分类文件里。
以下我们通过三个分类样例来看一下怎样使用分类:
接着我们上一篇的代码,以下我们创建一个Student的Test分类。创建步骤例如以下:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZGF3YW5nYW5iYW4=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
注意上面的Student+Test.h和Student+Test.m这两个文件就是我们为Student类创建的分类文件
Student+Test.h文件
#import "Student.h" @interface Student (Test) - (void)test; @end
Student+Test.m文件
#import "Student+Test.h" @implementation Student (Test) - (void) test {
NSLog(@"调用了Student的Test分类的test方法");
} @end
main.m文件
#import <Foundation/Foundation.h>
#import "Student.h"
#import "Student+Test.h" int main(int argc, const char * argv[]) {
@autoreleasepool {
Student *stu = [[[Student alloc] initStudent:23] autorelease];
[stu test];
}
return 0;
}
执行结果:
2014-11-16 11:32:00.861 内存管理[582:33690] 年龄为23的学生被创建了
2014-11-16 11:32:00.862 内存管理[582:33690] 调用了Student的Test分类的test方法
2014-11-16 11:32:00.862 内存管理[582:33690] 年龄为23的学生被释放了
分类除了这样的写法事实上能够直接写到Student.h和Student.m中,而不用单独创建文件。
我们也能够给系统的类(NSString)进行分类,比方我们给NSString加入一个处理json的方法。
#import <Foundation/Foundation.h> @interface NSString (JSON) + (void)json; @end
#import "NSString+JSON.h" @implementation NSString (JSON) + (void) json{
NSLog(@"{'nam':'CodeingSnal', 'age',24");
} @end
分类的使用场景:
1、在定义类的某些情况下(比如需求变更)。你可能须要给当中的某个或某几个类加入新的方法。
2、一个类中包括了很多不同种类的方法须要实现。而这些方法须要不同的团队的成员实现。
3、在使用基础类库的类时,有可能希望这些类实现一些自己须要的方法,比方写个NSString+JSON.h,至NSString这个类是开发一些解决JSON方法。
版权声明:本文博客原创文章,博客,未经同意,不得转载。
Objective-C基调(4)Category的更多相关文章
- Objective-C( Category 分类,非正式协议,分类延展)
分类: 主要是给原来类增加一些方法;不可以增加成员变量 Person (Person+eat) 非正式协议: 非正式协议就是类别,即凡是NSObject或其子类Foundation框架中的类增加的类别 ...
- Automake
Automake是用来根据Makefile.am生成Makefile.in的工具 标准Makefile目标 'make all' Build programs, libraries, document ...
- iOS 面试集锦
是第一篇: 1.Difference between shallow copy and deep copy? 浅复制和深复制的区别? 答案:浅层复制:只复制指向对象的指针,而不复制引用对象本身. 深层 ...
- IOS总结
1.Difference between shallow copy and deep copy? 浅复制和深复制的区别? 答案:浅层复制:只复制指向对象的指针,而不复制引用对象本身. 深层复制:复制引 ...
- 100道iOS面试题
面试题: 1__weak什么时候用 想要在block内部变外部变量需要加__weak或者__block 2.是否使用过coreImage和coreText?如果使用过,说说你的体验(答案在另一份) 3 ...
- objective c, category 和 protocol 中添加property
property的本质是实例变量 + getter 和 setter 方法 category和protocol可以添加方法 category 和 protocol中可以添加@property 关键字 ...
- Objective的字符串拼接 似乎没有Swift方便,但也可以制做一些较为方便的写法
NSString *str1 = @"字符串1"; NSString *str2 = @"字符串2"; //在同样条件下,Objective的字符串拼接 往往只 ...
- Objective C运行时(runtime)
#import <objc/runtime.h> void setBeingRemoved(id __self, SEL _cmd) { NSLog(@"------------ ...
- 刨根问底Objective-C Runtime
http://chun.tips/blog/2014/11/05/bao-gen-wen-di-objective%5Bnil%5Dc-runtime-(2)%5Bnil%5D-object-and- ...
随机推荐
- JDK源码学习系列05----LinkedList
JDK源码学习系列05----LinkedList 1.LinkedList简介 LinkedList是基于双向链表实 ...
- abstract修改方法
abstract这种方法修饰,主要用在抽象类和抽象方法. 抽象的类是不可实例化的比如 public abstract class Test{ } 他能够含有抽象的方法 public abstract ...
- MFC漆摘要-截图,获得DIB/DDB图形Pixel
1. 当前Screen进行Copy屏幕,获得BITMAP 当前屏幕Copy.须要获取当前屏幕的HDC, 一种是直接从屏幕DC抓原始图. 一种是然后使用兼容MemDC进行抓图,然后能够附加图 ...
- UVA 674 (入门DP, 14.07.09)
Coin Change Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We ...
- 左右PHP自增力、神秘递减操作
首先看一个面试题: $a = 1; $b = &$a; if ($b == $a++) echo "true"; else echo "false"; ...
- HDU 4777 Rabbit Kingdom(树状数组)
HDU 4777 Rabbit Kingdom 题目链接 题意:给定一些序列.每次询问一个区间,求出这个区间和其它数字都互质的数的个数 #include <cstdio> #include ...
- mysqlbackup 还原特定的表
mysqlbackup使用TTS恢复指定表. ************************************************************* 4.恢复特定表 ******* ...
- YT新人之巅峰大决战04
Problem Description Eddy's interest is very extensive, recently he is interested in prime number. Ed ...
- 解决mysqldb查询大量数据导致内存使用过高的问题
1.源码 connection=MySQLdb.connect( host="thehost",user="theuser", passwd="the ...
- ZenCoding for EmEditor Snippets 的安装
ZenCoding for EmEditor的安装 你可以从这里下载所需文件Library under the Snippets category.安装前请确认你的EmEditor内置有代码片段(Sn ...