Objective-C:分类(Category、extension)

分类(Category 、Extension)
(一)分类的划分
 
  (2) 1、(命名的类别)类别Category:只能添加新的方法,不能添加新变量。
  
       2、(未命名的类别)类的扩展Extension://扩展一般直接写在类的实现文件中

                                       //扩展中定义的都是类中私有的实例变量和方法       

(二)分类的使用

(1)分类只能增加方法(包括类方法和对象方法),不能增加成员变量

(2)在分类方法的实现中可以访问原来类中的成员变量;

(3)分类中可以重新实现原来类中的方法,但是会覆盖掉原来的方法,导致原来的方法无法再使用(警告);

(4)方法调用的优先级:分类->原来的类->父类,若包含有多个分类,则最后参与编译的分类优先;

(5)在很多的情况下,往往是给系统自带的类添加分类,如NSObject和NSString,因为有的时候,系统类可能并不能满足我们的要求。

(6)在大规模的应用中,通常把相应的功能写成一个分类,可以有无限个分类,对原有类进行扩充,一般分模块写,一个模块一个分类。

 //自定义的字符串类String

 1 //  String.h
 2 //  类别
 3 //
 4 //  Created by ma c on 15/8/12.
 5 //  Copyright (c) 2015年. All rights reserved.
 6 //
 7 
 8 #import <Foundation/Foundation.h>
 9 
10 @interface String : NSObject
11 @property(nonatomic,strong)NSString* string;
12 -(id)initWithString:(NSString*) str;
13 -(void)print;
14 @end
 1 //  String.m
 2 //  类别
 3 //
 4 //  Created by ma c on 15/8/12.
 5 //  Copyright (c) 2015年. All rights reserved.
 6 //
 7 
 8 #import "String.h"
 9 //#import "String_String_private.h"//在String类单独导入一个类扩展文件
10 
11 //类扩展extension,添加的属性str1和show方法都是私有的,只能在String类中可以访问得到
12 @interface String()//直接将类扩展文件写入String类的实现中
13 {
14     NSString *str1;
15 }
16 -(void)show;
17 @end
18 
19 
20 @implementation String
21 -(id)initWithString:(NSString*) str
22 {
23     self = [super init];
24     if(self)
25     {
26         _string = str;
27     }
28     return self;
29 }
30 -(void)show
31 {
32     NSLog(@"string is runing");
33 }
34 -(void)print//通过print方法访问到类扩展中添加的属性和方法
35 {
36     [self show];
37     NSLog(@"%@",_string);
38 }
39 @end

 

//单独的类扩展文件

 1 //  String_String_private.h
 2 //  类别
 3 //
 4 //  Created by ma c on 15/8/12.
 5 //  Copyright (c) 2015年. All rights reserved.
 6 //
 7 
 8 #import "String.h"
 9 
10 @interface String ()
11 {
12     NSString *str1;
13 }
14 //@property(nonatomic,strong)NSString* str1;
15 -(void)show;
16 @end

 

//类别文件

 1 //  String+Count.h
 2 //  类别
 3 //
 4 //  Created by ma c on 15/8/12.
 5 //  Copyright (c) 2015年. All rights reserved.
 6 //
 7 
 8 #import "String.h"
 9 //String的类别Count,添加的方法,对String类进行功能的扩展(切记:类别中只能添加方法)
10 @interface String (Count)
11 -(NSInteger) nmuberOfString: (NSString *)str;
12 -(NSInteger) numberOfCount: (NSString *)str;
13 @end
 1 //  String+Count.m
 2 //  类别
 3 //
 4 //  Created by ma c on 15/8/12.
 5 //  Copyright (c) 2015年. All rights reserved.
 6 //
 7 
 8 #import "String+Count.h"
 9 
10 @implementation String (Count)
11 -(NSInteger) nmuberOfString: (NSString *)str
12 {
13     NSInteger count = 0;
14     for(int i=0;i<str.length;i++)
15     {
16         unichar ch = [str characterAtIndex:i];
17         if(ch>='0' && ch<='9')
18             count++;
19     }
20     return count;
21 }
22 -(NSInteger) numberOfCount: (NSString *)str
23 {
24     NSInteger count = 0;
25     for(int i=0;i<str.length;i++)
26     {
27         unichar ch = [str characterAtIndex:i];
28         if(ch>='0' && ch<='9')
29             count++;
30     }
31     return count;
32 }
33 @end

 

//主函数测试

 1 //  main.m
 2 //  类别
 3 //
 4 //  Created by ma c on 15/8/12.
 5 //  Copyright (c) 2015年. All rights reserved.
 6 //
 7 
 8 #import <Foundation/Foundation.h>
 9 #import "String+Count.h"
10 #import "String.h"
11 int main(int argc, const char * argv[])
12 {
13     @autoreleasepool
14     {
15        //测试类扩展(未命名的类别)   关键字:extension
16        String *string = [[String alloc]initWithString:@"xiayuan"];
17       [string print];
18         
19         
20        //测试类别(命名的类别)      关键字:Category
21        NSInteger a = [string nmuberOfString:@"xu3984jjsn6ew32"];
22        NSLog(@"a = %ld",a);
23         
24        NSInteger b = [string numberOfCount:@"36y3jfse93u4gdf8"];
25        NSLog(@"b = %ld",b);
26     }
27     return 0;
28 }

//运行结果

2015-08-12 15:24:06.281 类别[1304:75695] string is runing

2015-08-12 15:24:06.282 类别[1304:75695] xiayuan

2015-08-12 15:24:06.282 类别[1304:75695] a = 7

2015-08-12 15:24:06.282 类别[1304:75695] b = 7

Program ended with exit code: 0

posted @ 2015-08-12 15:48  XYQ全哥  阅读(384)  评论(0编辑  收藏  举报