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;
}
定义: 下面演示在人的基础扩展为学生
定义类
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;
}
相关文章推荐
- for语句练习 阶乘
- 数学上的一些算法
- light--oj--1294-- Positive Negative Sign(数学规律)
- OC控件间的继承关系,用UI实现
- leetcode Word Pattern
- 计蒜课—和为M的组合个数
- Openfire on Centos7
- CSS3之position
- poj--1637--Sightseeing tour(网络流,最大流判断混合图是否存在欧拉图)
- 按键精灵V1.0版
- 【练习手记】【多题合集】用树状数组做线段树练习1、2、3
- 开源界何时不再玩捉迷藏游戏?
- project euler 46
- poj--1637--Sightseeing tour(网络流,最大流判断混合图是否存在欧拉图)
- Ubuntu更新数据源
- 解决This function or variable may be unsafe的方法
- qt+opencv3.0配置详解
- project euler 44
- Matlab 最大值滤波 中值滤波 最小值滤波 函数 ordfilt2
- php-redis扩展编译