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的更多相关文章

  1. Objective-C( Category 分类,非正式协议,分类延展)

    分类: 主要是给原来类增加一些方法;不可以增加成员变量 Person (Person+eat) 非正式协议: 非正式协议就是类别,即凡是NSObject或其子类Foundation框架中的类增加的类别 ...

  2. Automake

    Automake是用来根据Makefile.am生成Makefile.in的工具 标准Makefile目标 'make all' Build programs, libraries, document ...

  3. iOS 面试集锦

    是第一篇: 1.Difference between shallow copy and deep copy?
浅复制和深复制的区别?
答案:浅层复制:只复制指向对象的指针,而不复制引用对象本身.
深层 ...

  4. IOS总结

    1.Difference between shallow copy and deep copy?
浅复制和深复制的区别?
答案:浅层复制:只复制指向对象的指针,而不复制引用对象本身.
深层复制:复制引 ...

  5. 100道iOS面试题

    面试题: 1__weak什么时候用 想要在block内部变外部变量需要加__weak或者__block 2.是否使用过coreImage和coreText?如果使用过,说说你的体验(答案在另一份) 3 ...

  6. objective c, category 和 protocol 中添加property

    property的本质是实例变量 + getter 和 setter 方法 category和protocol可以添加方法 category 和 protocol中可以添加@property 关键字 ...

  7. Objective的字符串拼接 似乎没有Swift方便,但也可以制做一些较为方便的写法

    NSString *str1 = @"字符串1"; NSString *str2 = @"字符串2"; //在同样条件下,Objective的字符串拼接 往往只 ...

  8. Objective C运行时(runtime)

    #import <objc/runtime.h> void setBeingRemoved(id __self, SEL _cmd) { NSLog(@"------------ ...

  9. 刨根问底Objective-C Runtime

    http://chun.tips/blog/2014/11/05/bao-gen-wen-di-objective%5Bnil%5Dc-runtime-(2)%5Bnil%5D-object-and- ...

随机推荐

  1. android studio下gradle与Git错误解决方法

    Error: Gradle: Execution failed for task ':mytask' > A problem occurred starting process 'command ...

  2. 重新想象 Windows 8 Store Apps (13) - 控件之 SemanticZoom

    原文:重新想象 Windows 8 Store Apps (13) - 控件之 SemanticZoom [源码下载] 重新想象 Windows 8 Store Apps (13) - 控件之 Sem ...

  3. 同一路由器不同vlan之间的通信(一)

    还是废话不多说,第一步,看拓扑图. 先把pc上的ip都配好.開始设置 switch0: >en >conf t >vlan 2 >exit >int fa 0/1 > ...

  4. ios-上拉电阻负载许多其他接口

    想尝试拉加载意识到有多少开始了他的研究之旅,我看了两天做出最终的界面. 之所以这么慢是由于,我不知道要将上拉出现的view放在哪.就能在scrollView拉究竟部的时候被拉出来.还有就是怎么拉出来之 ...

  5. ZOJ - 3822 Domination (DP)

    Edward is the headmaster of Marjar University. He is enthusiastic about chess and often plays chess ...

  6. org.apache.subversion.javahl.ClientException: svn: E155021: This client is too old to work with the

    org.apache.subversion.javahl.ClientException: svn: E155021: This client is too old to work with the ...

  7. [LeetCode119]Pascal's Triangle II

    题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [ ...

  8. T-Sql(一)简单语法

    原文:T-Sql(一)简单语法 Sql Server是鄙人学习的第一种数据库,对Sql Server有一种特别的情感,下面就说一下Sql Server的简单语法,适用初学者. 1,创建数据库creat ...

  9. iOS 7 新特性

      iOS7更新了很多引人注目的功能.用户界面完全重新设计了.iOS7为开发2D,2.5D游戏引入了全新的动画系统.加强多线程,点对点连接,以及许多其他重要的功能让iOS7成为有史以来最有意义的一次发 ...

  10. Windows 8实例教程系列 - 数据绑定高级实例

    原文:Windows 8实例教程系列 - 数据绑定高级实例 上篇Windows 8实例教程系列 - 数据绑定基础实例中,介绍Windows 8应用开发数据绑定基础,其中包括一些简单的数据绑定控件的使用 ...