首页 > 学院 > 开发设计 > 正文

分类Category

2019-11-06 07:17:47
字体:
来源:转载
供稿:网友

如何修个一个无法得到源码的类的方法? 如何把类的方法分组? 如何把类的实现分散到多个源文件?

分类的定义

#import"原类名.h"@interface 原类名(分类名)//声明方法(不能有实例变量)@end#import"原类名+分类名.h"@implementation 原类名(分类名)//声明方法(不能有实例变量)@end

分类和继承的对比:

分类只能有属性和方法,不能增加实例变量,需要扩展方法同时不需要扩展实例变量时,用分类比较好在分类中可以以相同的方法名覆盖原类方法

无名分类

Complex.m--------------------#import "Complex.h"//无名分类在类的实例文件.m中实现,一个类只能有一个无名分类,同样不能有数据成员,其所有的属性和成员函数都为私有,类外无法访问@interface Complex()@PRoperty(nonatomic) double a;-(void)fun;@end-----------------------------@implementation Complex@synthesize real,imag;-(id)initWithReal:(double)aReal imag:(double)aImag{ if(self = [super init]){ real = aReal; imag = aImag; } return self;}-(void)print{ NSLog(@"%g,%g",real,imag);}-(void)fun{}@end
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表