您的位置:首页 > 其它

OC 类别(分类)Categroy

2015-12-04 21:31 330 查看
Categroy类别,又称为扩展类,在类的原基础上扩展方法,且不可添加变量,如果扩展的方法与原始类中的方法相同,则会隐藏原始方法,且不可在扩展方法中通过super调用原始方法,这里与继承不同。

定义: 下面演示在人的基础扩展为学生

定义类

Person.h

[objc] view plaincopy





#import <Foundation/Foundation.h>

@interface Person : NSObject

@property(retain) NSString *name;

@property(nonatomic,assign) int age;

@end

Person.m

[objc] view plaincopy





#import "Person.h"

@implementation Person

@end

扩展

Person+Student.h 约定,类别的文件名为 扩展类+类别名.h

[objc] view plaincopy





#import "Person.h"

@interface Person (Student)

-(void)studing;

@end

Person+Student.m

[objc] view plaincopy





#import "Person+Student.h"

@implementation Person (Student)

-(void)studing{

NSLog(@"学习ing");

}

@end

main.m

[objc] view plaincopy





#import <Foundation/Foundation.h>

#import "Person+Student.h"//导入时导入扩展的那个文件

int main(int argc, const charchar * argv[])

{

@autoreleasepool {

Person *p = [[Person alloc] init];

[p studing];

}

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: