我是靠谱客的博主 可爱星月,这篇文章主要介绍类别(Category)的作用(一)---将实现分散到不同文件,现在分享给大家,希望可以做个参考。

在上一篇文章 类别(Category)的作用(零)中,大概说明类别是什么东西,接下来详细说明类别的第一个作用。


类别作用一:将类的实现分散到多个不同文件或多个不同框架中。


一、概念:

在使用类的时候,我们可以将类的接口放入头文件中,而将类的实现放入.m文件中。但不可以将@implementation分散到多个不同的.m文件中。


二、代码说明:

头文件CatagoryThing.h包含类的声明和一些类别,导入Foundation框架,然后带有3个整型变量的声明

复制代码
1
2
3
4
5
6
7
#import<Foundation/Foundation.h> @interface CategoryThing : NSObject { int thing1; int thing2; int thing3; } @end


类声明之后是3个类别,每个类别具有一个实例变量的访问器,将这些实现分散到不同的文件中.

复制代码
1
2
3
4
@interface CategoryThing(Thing1) - (void) setThing1: (int) thing1; - (int) thing1; @end
复制代码
1
2
3
4
@interface CategoryThing (Thing2) - (void) setThing2: (int) thing2; - (int) thing2; @end
复制代码
1
2
3
4
@interface CategoryThing (Thing3) - (void) setThing3: (int) thing3; - (int) thing3; @end


类别可以访问其继承的类的实例变量,类别的方法具有最高的优先级。

类别可以分散到不同文件中,甚至不同框架中。


文章参考:
1、http://blog.csdn.net/jiajiayouba/article/details/21105873
2、http://www.cnblogs.com/stevenwuzheng/p/5457487.html
3、http://blog.csdn.net/wzzvictory/article/details/9295317

最后

以上就是可爱星月最近收集整理的关于类别(Category)的作用(一)---将实现分散到不同文件的全部内容,更多相关类别(Category)内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(76)

评论列表共有 0 条评论

立即
投稿
返回
顶部