OC语法9——Category类别

Category(分类):

  当我们在开发过程中要给类添加新的方法时,一般不要去动原类。

  再不改动原类的限制下,怎么拓展类的方法?以往我们的做法是新建子类使其继承该类,然后通过子类拓展类的行为。

  OC提供了一种全新的方法:Category(分类)。在不改动原类的基础上动态的拓展类的行为。

  假如我们要动态拓展Student类,则应该建一个分类(Category),注意:文件命名要有规范,Student+StuOthers.h

  格式:只要在被扩展的类名(原类)后加(),在括号里写分类名(拓展类名);

    Student+StuOthers.h中

#import "Student.h"
@interface Student (StuOthers)   //括号里就是分类名
- void test();
@end

    Student+StuOthers.m中

#import "Student+StuOthers.h"
@implementation Student (StuOthers)
- void test()
{
    @NSLog(@"wanger");
}
@end
原文地址:https://www.cnblogs.com/wangerxiansheng/p/4297296.html