黑马程序员技术交流社区
标题:
分类Category_总结
[打印本页]
作者:
huh
时间:
2015-12-25 23:46
标题:
分类Category_总结
是什么?
把一个类分成多个部分
有什么用?
在不改变原有类的基础上,给原有类,添加功能(方法)! 有利于多人之间的协同开发!
给系统提供的类,添加额外的功能!
怎么用?
Xcode里面的使用:
选择: Object -C File什么时候用?
场景1:在已写好类的基础上,想要不改变原有类的同时,添加新的方法?
1)用继承!
2)用分类Category
用的时候需要注意什么?
在分类里面有本类相同名称的方法时候,需要注意,编译的顺序!后一次编译的会覆盖前面一次编译!
更改编译的顺序方法
特殊:
一般
分类中的方法和本类的方法相同
,
则最后一次的方法
,
将覆盖前面一次的方法
!
特殊
分类中的
load
方法
,
不会覆盖本类的
load
方法,
分类中子类的
load
方法
,
同样不会覆盖父类和本类的
load
方法
//
// Person.h
//
#import <Foundation/Foundation.h>
@interface Person : NSObject
{
NSString* _name;
}
@property NSString * name;
-(id)initPerson:(NSString *)name;
-(void)info;
@end
//
// Person.m
//
#import "Person.h"
#import "Person.h"
@implementation Person
-(id) initPerson:(NSString *)name{
if (self = [super init]) {
_name = name;
}
return self;
}
-(void)info{
NSLog(@"Person的Info方法!");
}
@end
//
// Student.h
//
#import "Person.h"
@interface Student : Person
-(void) studentInfo;
@end
//
// Student.m
//
//
#import "Student.h"
@implementation Student
/*
场景1:在已写好类的基础上,想要不改变原有类的同时,添加新的方法?
第一种方案:继承父类,将需要添加的功能,写在子类里面!
*/
-(void) studentInfo{
[super name];
NSLog(@"学生在学习...");
}
@end
//
// Person+student.h
//
#import "Person.h"
@interface Person (student)
//分类里面不能写属性
//@property int age;
-(void) stuInfo;
//Person分类,重写Person的Info方法
-(void)info;
@end
//
// Person+student.m
//
#import "Person+student.h"
@implementation Person (student)
/*
场景1:在已写好类的基础上,想要不改变原有类的同时,添加新的方法?
第二种方案:使用分类
注意格式: 命名:原有类名+分类名
声明: @interface 原有类名(分类名)
实现: @implementation 原有类名(分类名)
*/
-(void)stuInfo{
NSLog(@"学习,学习...");
}
-(void)info{
NSLog(@"person的分类student的方法!");
}
@end
//
// Person+sport.h
//
#import "Person.h"
@interface Person (sport)
-(void)info;
@end
//
// Person+sport.m
//
#import "Person+sport.h"
@implementation Person (sport)
-(void)info{
//调用本类的属性,但是属性修饰符>=@protected
NSLog(@"Person的分类sport的方法,身高:%@",_name);
}
@end
/*
NSString+getCount.h
场景2:给系统类NSString,添加方法
查看字符对象中,出现a的次数
*/
#import <Foundation/Foundation.h>
@interface NSString (getCount)
//-(int)getCount:(NSString *)str;
-(int)getcount;
@end
/*
NSString+getCount.m
场景2:给系统类NSString,添加方法
查看字符对象中,出现a的次数?
1,取出字符对象的每个元素
2,进行比较
*/
#import "NSString+getCount.h"
@implementation NSString (getCount)
/*
-(int)getCount:(NSString *)str{
int count = 0;
for (int i = 0; i<str.length; i++) {
unichar uc = [str characterAtIndex:i];//取出每个元素
if(uc =='a'){
count ++;
}
}
return count;
}
*/
-(int)getcount{
int count = 0;
for (int i = 0 ;i<self.length; i++) {
unichar uc = [self characterAtIndex:i];
if(uc == 'a'){
count ++;
}
}
return count;
}
@end
/*
main.m
什么是分类Category?
把一个类分成多个部分
分类有什么好处?
1)在不改变类的情况下,给类添加方法
2)协同开发,优化类的结构
3)给系统提供的类,添加额外的功能
继承和分类的区别?
相同点:
分类和继承都可以在不改变类的同时,给类添加方法
不同点:
分类在调用时候,直接调用就可以!
继承在调用的时候,需要强制转换!
需要注意什么?
1)分类里面不能写属性
2)分类里面不能用super
3)分类里面的方法,最好不要跟主类或其他分类的方法相同,
否则最后编译的方法,将会覆盖前面的方法!
4)分类中可以直接使用主类的属性,但是属性必须是>=@protected
编译器编译的顺序:
父类--> 主类 --> 分类
*/
#import <Foundation/Foundation.h>
#import "Person.h"
#import "Student.h"
#import "Person+student.h"
#import "NSString+getCount.h"
int main(int argc, const char * argv[]) {
Person *per = [[Person alloc]initPerson:@"huh"];
NSLog(@"name =%@",per.name);
// 场景1:在已写好类的基础上,想要不改变原有类的同时,添加新的方法?
// 第一种,子类继承父类,将新添的方法写在子类里面(studentInfo)
Person *per1 = [Student new];
Student *stu = (Student *)per1;
[stu studentInfo];
// 第二种,采用分类方式,分类调用方法不需要强转,直接调用就可!
[per stuInfo];
[per1 stuInfo];
/*分类里面不能写属性
stu.age =10;
NSLog(@"age = %d",stu.age);*/
/*
两个输出的结果相同,都是分类的方法
因为,分类中跟本类方法名相同的方法,
后一次会覆盖上次的方法!
per1.name = @"huh";
per1.info;
stu.info;
*/
// 场景2:给系统类NSString,添加方法
// 查看字符对象中,出现a的次数
// 第一种方式
// NSString * str = @"jdlsjaaqwioaaeda";
// NSLog(@"a出现的次数%d",[str getCount:str]);
// 第二种方式(简洁)
NSString * str = @"jaadalasjaaqwioaeda";
NSLog(@"a出现的次数%d",str.getcount);
int count = [str getcount];
NSLog(@"a出现的次数%d",count);
return 0;
}
复制代码
file:///var/folders/7h/xb292dy96z57vt0fp8wpzvfm0000gn/T/WizNote/ed3f4cc2-c138-4c1e-ba8f-cd61e7723df0.png
file:///var/folders/7h/xb292dy96z57vt0fp8wpzvfm0000gn/T/WizNote/ed3f4cc2-c138-4c1e-ba8f-cd61e7723df0.png
1.png
(23.41 KB, 下载次数: 0)
下载附件
2015-12-25 23:42 上传
2.png
(24.31 KB, 下载次数: 0)
下载附件
2015-12-25 23:43 上传
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2