Objective-c_3_features

我们都知道,面向对象程序设计中的类有三大特性:继承,封装,多态,这三大特性是学习类的时候,必须理解的问题,既是基础,更该给予重视。

一、封装(Encapsulation)

封装就是对类中的一些字段、方法进行保护,不被外界访问到,有一种权限的控制能力,Java中有四种访问权限修饰符:public、default、protected、private,它们的访问权限依次递减,这样我们在定义类的时候,哪些字段和方法不想暴露出去,哪些字段和方法可以暴露,可以通过不同修饰符来区分,这就是封装。

举例说明:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52



#import







@interface Car : NSObject {



// 这个属性就是对外进行保密的相当于private,所以我们需要在外部访问的话,必须定义get/set方法



// 默认的是private的,但是我们可以使用@public设置为public属性的,那么在外部可以直接访问:



person->capcity = 2.8;



// 当然我们一般不这么使用,因为这会破坏封装性,这种用法相当于C中的结构体中权限



// 一共四种:@public,@protected,@private,@package,权限依次递减,这个和Java中是相同的



@public



float _capcity; //油量属性



}



- (void)run;



@end

这里我们可以看到,OC中也有四种权限修饰符:@public、@protected、@private、@package

其中默认的修饰符是@private。

但是这里要注意的是,OC中的方法是没有修饰符的概念的,这与其它语言有所区别,一般都是公开访问的,即public的。在OC中,若需要做到让一个方法不能被外界访问,只需要在.m文件中实现这个方法,不要在图文件中进行定义,简言之,就是该方法有实现而没有定义,这样外界在倒入头文件的时候,是没有这个方法的,这个方法只可以在自己的.m文件中使用。

二、继承

继承是类中的一个重要特性,其作用在于避免写重复的代码,可重用性很高,继承的目的是为了减少代码的冗余,还是DRY原则(don`t repeat yourself)。

在Car.h中,在Car类中定义了两个属性,以及一些方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52



#import







@interface Car : NSObject{



NSString *_brand;



NSString *_color;



}







- (void)setBrand:(NSString *)brand;



- (void)setColor:(NSString *)color;



- (void)brake;



- (void)quicken;







@end

在Car.m方法中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64



#import "Car.h"







@implementation Car



- (void)setBrand:(NSString *)brand{



_brand = brand;



}



- (void)setColor:(NSString *)color{



_color = color;



}



- (void)brake{



NSLog(@"刹车");



}



- (void)quicken{



NSLog(@"加速");



}



@end

在其子类Taxi中,我们可以对Car的方法进行继承。

Taxi.h文件中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40



#import "Car.h"







@interface Taxi : Car{



NSString *_company;//所属公司



}







//打印发票



- (void)printTick;







@end

Taxi类继承了父类Car,这里需要导入父类的头文件,然后在Taxi类中多了一个属性和方法,与父类一致的方法则继承或改写。

Taxi.m文件中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40



#import "Taxi.h"



@implementation Taxi







- (void)printTick{



[super brake];



[self brake];



NSLog(@"%@出租车打印了发票,公司为:%@,颜色为:%@",_brand,_company,_color);



}







@end

对方法的实现,这里我们看到实现文件中是不需要导入父类Car的头文件的,因为可以认为,Taxi.h头文件中已经包含了Car的头文件了。而且,这里可以使用super关键字来调用父类的方法,同时这里我们也是可以用self关键字来调用,这里看到其实这两种方式调用的效果是一样的,当我们在子类重新实现brake方法的时候(Java中的重写概念),那么这时候super关键字调用的还是父类的方法,而self调用的就是重写之后的brake方法了。同样,我们也是可以使用父类中的属性。

再看一下另外一个子类Truck:

在Truck.h中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52



#import "Car.h"



//卡车类继承Car



@interface Truck : Car{



float _maxWeight;//最大载货量



}







//覆盖父类的方法brake



//优先调用子类的方法



- (void)brake;







- (void)unload;







@end

这里就自己定义了一个brake方法,这时候就会覆盖父类中的brake方法了。

在Truck.m中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68







#import "Truck.h"







@implementation Truck







- (void)brake{



[super brake];



NSLog(@"Truck类中的brake方法");



}







- (void)unload{



[super brake];//调用父类的方法



[self brake];//也是可以的



NSLog(@"%@的卡车卸货了,载货量:%.2f,汽车的颜色:%@",_brand,_maxWeight,_color);



}







@end

这里就可以看到,我们会在brake方法中调用一下父类的brake方法,然后在实现我们自己的逻辑代码。

三、多态

多态对于面向对象来说,个人感觉真的很重要,其对于编写代码的优雅方式也起到了很重要的作用。在很多设计模式中大部分都是用到了多态的特性,Java中的多态特性用起来很是方便的,但是C++中就很难用了,其实多态说白了就是:定义类型和实际类型,一般是基于接口的形式实现的。

以打印机为例:

写一个抽象的打印机类Printer,

Printer.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28



#import







@interface Printer : NSObject







- (void) print;







@end

Printer.m:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38





#import "Printer.h"







@implementation Printer







- (void)print{



NSLog(@"打印机打印纸张");



}







@end

实现也是很简单的,下面来看一下具体的子类ColorPrinter:

ColorPrinter.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30





#import "Printer.h"







//修改父类的打印行为



@interface ColorPrinter : Printer



- (void)print;



@end



ColorPrinter.m

ColorPrinter.m:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38





#import "ColorPrinter.h"







@implementation ColorPrinter







- (void)print{



NSLog(@"彩色打印机");



}







@end

再看一下另一个子类:

BlackPrinter.m:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36



#import "BlackPrinter.h"







@implementation BlackPrinter







- (void)print{



NSLog(@"黑白打印机");



}







@end

这里我们在定义一个Person类,用来操作具体的打印机。

Person.m:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76



#import "Person.h"







@implementation Person







/*



- (void) printWithColor:(ColorPrinter *)colorPrint{



[colorPrint print];



}







- (void) printWithBlack:(BlackPrinter *)blackPrint{



[blackPrint print];



}



*/







- (void) doPrint:(Printer *)printer{



[printer print];



}







@end

再来看一下测试代码:

Person.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76



#import "Person.h"







@implementation Person







/*



- (void) printWithColor:(ColorPrinter *)colorPrint{



[colorPrint print];



}







- (void) printWithBlack:(BlackPrinter *)blackPrint{



[blackPrint print];



}



*/







- (void) doPrint:(Printer *)printer{



[printer print];



}







@end

Person.m:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116



#import "Person.h"



#import "BlackPrinter.h"



#import "ColorPrinter.h"







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



@autoreleasepool {



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



ColorPrinter *colorPrint = [[ColorPrinter alloc] init];



BlackPrinter *blackPrint = [[BlackPrinter alloc] init];



//多态的定义



/*



Printer *p1 = [[ColorPrinter alloc] init];



Printer *p2 = [[BlackPrinter alloc] init];



[person doPrint:p1];



[person doPrint:p2];



*/



//通过控制台输入的命令来控制使用哪个打印机



int cmd;



do{



scanf("%d",&cmd);



if(cmd == 1){



[person doPrint:colorPrint];



}else if(cmd == 2){



[person doPrint:blackPrint];



}



}while (1);



}



return 0;



}

下面就来详细讨论一下多态的好处,以上例子中一个彩色打印机和黑白打印机,Person类中有一个操作打印的方法,当然这个方法是需要打印机对象的,如果不用多态机制实现的话(Person.h中注释的代码部分),就是给两种打印机单独定义个操作的方法,然后在Person.m(代码注释的部分)中用具体的打印机对象进行操作,在main.m文件中,我们看到,当person需要使用哪个打印机的时候,就去调用指定的方法:

1
2
3
4
5
6
7
8



[person printWithBlack:blackPrint];//调用黑白打印机



[person printWithColor:colorPrint];//调用彩色打印机

假如现在又需要另一种打印机 ,那么我们还需要在Person.h中定义一种操作这种打印机的方法,那么后续如果添加新的打印机呢?还要继续添加方法吗?那么Person.h文件就会变得臃肿。所以此时多态就体现了优势,使用父类类型,在Person.h中定义一个方法- (void) doPrint:(Printer *)printer;就可以了。这里可以看出,这个方法的参数类型就是父类类型,实际类型为子类类型,其方法体为:

1
2
3
4
5
6
7
8
9
10
11
12



- (void) doPrint:(Printer *)printer{



[printer print];



}

这里调用print方法,就是传递进来的实际类型的print方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22





Printer *p1 = [[ColorPrinter alloc] init];



Printer *p2 = [[BlackPrinter alloc] init];







[person doPrint:p1];



[person doPrint:p2];

这里的p1,p2表面上的类型是printer,但实际类型是子类类型,所以调用他们自己对应的print方法。