OC三大特性之一:多态的介绍

来源:互联网 发布:手机免费注册淘宝店铺 编辑:程序博客网 时间:2024/05/20 23:29

 

三、多态

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

打印机的例子

抽象的打印机类Printer

Printer.h

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //  
  2. //  Printer.h  
  3. //  07_DynamicDemo  
  4. //  
  5. //  Created by jiangwei on 14-10-11.  
  6. //  Copyright (c) 2014年 jiangwei. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10.   
  11. @interface Printer : NSObject  
  12.   
  13. - (void) print;  
  14.   
  15. @end  
就是一个简单的方法print

Printer.m

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //  
  2. //  Printer.m  
  3. //  07_DynamicDemo  
  4. //  
  5. //  Created by jiangwei on 14-10-11.  
  6. //  Copyright (c) 2014年 jiangwei. All rights reserved.  
  7. //  
  8.   
  9. #import "Printer.h"  
  10.   
  11. @implementation Printer  
  12.   
  13. - (void)print{  
  14.     NSLog(@"打印机打印纸张");  
  15. }  
  16.   
  17. @end  
实现也是很简单的


下面来看一下具体的子类

ColorPrinter.h

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //  
  2. //  ColorPrinter.h  
  3. //  07_DynamicDemo  
  4. //  
  5. //  Created by jiangwei on 14-10-11.  
  6. //  Copyright (c) 2014年 jiangwei. All rights reserved.  
  7. //  
  8.   
  9. #import "Printer.h"  
  10.   
  11. //修改父类的打印行为  
  12. @interface ColorPrinter : Printer  
  13. - (void)print;  
  14. @end  


ColorPrinter.m

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //  
  2. //  ColorPrinter.m  
  3. //  07_DynamicDemo  
  4. //  
  5. //  Created by jiangwei on 14-10-11.  
  6. //  Copyright (c) 2014年 jiangwei. All rights reserved.  
  7. //  
  8.   
  9. #import "ColorPrinter.h"  
  10.   
  11. @implementation ColorPrinter  
  12.   
  13. - (void)print{  
  14.     NSLog(@"彩色打印机");  
  15. }  
  16.   
  17. @end  

在看一下另外一个子类

BlackPrinter.h

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //  
  2. //  BlackPrinter.h  
  3. //  07_DynamicDemo  
  4. //  
  5. //  Created by jiangwei on 14-10-11.  
  6. //  Copyright (c) 2014年 jiangwei. All rights reserved.  
  7. //  
  8.   
  9. #import "Printer.h"  
  10.   
  11. @interface BlackPrinter : Printer  
  12.   
  13. - (void)print;  
  14.   
  15. @end  

BlackPrinter.m

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //  
  2. //  BlackPrinter.m  
  3. //  07_DynamicDemo  
  4. //  
  5. //  Created by jiangwei on 14-10-11.  
  6. //  Copyright (c) 2014年 jiangwei. All rights reserved.  
  7. //  
  8.   
  9. #import "BlackPrinter.h"  
  10.   
  11. @implementation BlackPrinter  
  12.   
  13. - (void)print{  
  14.     NSLog(@"黑白打印机");  
  15. }  
  16.   
  17. @end  


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

Person.h

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //  
  2. //  Person.h  
  3. //  07_DynamicDemo  
  4. //  
  5. //  Created by jiangwei on 14-10-11.  
  6. //  Copyright (c) 2014年 jiangwei. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10.   
  11. #import "ColorPrinter.h"  
  12. #import "BlackPrinter.h"  
  13.   
  14. //扩展性不高,当我们需要添加一个新的打印机的时候还要定义对应的一个方法  
  15. //所以这时候就可以使用多态技术了  
  16.   
  17. @interface Person : NSObject{  
  18.     NSString *_name;  
  19. }  
  20.   
  21. //- (void) printWithColor:(ColorPrinter *)colorPrint;  
  22.   
  23. //- (void) printWithBlack:(BlackPrinter *)blackPrint;  
  24.   
  25. - (void) doPrint:(Printer *)printer;  
  26.   
  27. @end  


Person.m

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //  
  2. //  Person.m  
  3. //  07_DynamicDemo  
  4. //  
  5. //  Created by jiangwei on 14-10-11.  
  6. //  Copyright (c) 2014年 jiangwei. All rights reserved.  
  7. //  
  8.   
  9. #import "Person.h"  
  10.   
  11. @implementation Person  
  12.   
  13. /* 
  14. - (void) printWithColor:(ColorPrinter *)colorPrint{ 
  15.     [colorPrint print]; 
  16. } 
  17.  
  18. - (void) printWithBlack:(BlackPrinter *)blackPrint{ 
  19.     [blackPrint print]; 
  20. } 
  21.  */  
  22.   
  23. - (void) doPrint:(Printer *)printer{  
  24.     [printer print];  
  25. }  
  26.   
  27. @end  

再来看一下测试代码:

main.m

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //  
  2. //  main.m  
  3. //  07_DynamicDemo  
  4. //  
  5. //  Created by jiangwei on 14-10-11.  
  6. //  Copyright (c) 2014年 jiangwei. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10.   
  11. #import "Person.h"  
  12. #import "BlackPrinter.h"  
  13. #import "ColorPrinter.h"  
  14.   
  15. int main(int argc, const charchar * argv[]) {  
  16.     @autoreleasepool {  
  17.           
  18.         Person *person =[[Person alloc] init];  
  19.           
  20.         ColorPrinter *colorPrint = [[ColorPrinter alloc] init];  
  21.         BlackPrinter *blackPrint = [[BlackPrinter alloc] init];  
  22.           
  23.         //多态的定义  
  24.         /* 
  25.         Printer *p1 = [[ColorPrinter alloc] init]; 
  26.         Printer *p2 = [[BlackPrinter alloc] init]; 
  27.          
  28.         [person doPrint:p1]; 
  29.         [person doPrint:p2]; 
  30.          */  
  31.           
  32.         //通过控制台输入的命令来控制使用哪个打印机  
  33.         int cmd;  
  34.         do{  
  35.             scanf("%d",&cmd);  
  36.             if(cmd == 1){  
  37.                 [person doPrint:colorPrint];  
  38.             }else if(cmd == 2){  
  39.                 [person doPrint:blackPrint];  
  40.             }  
  41.         }while (1);  
  42.           
  43.     }  
  44.     return 0;  
  45. }  


下面就来详细讲解一下多态的好处

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

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. [person printWithBlack:blackPrint];//调用黑白打印机  
  2. [person printWithColor:colorPrint];//调用彩色打印机  
这种设计就不好了,为什么呢?假如现在又有一种打印机,那么我们还需要在Person.h中定义一种操作这种打印机的方法,那么后续如果在添加新的打印机呢?还在添加方法吗?那么Person.h文件就会变得很臃肿。所以这时候多态就体现到好处了,使用父类类型,在Person.h中定义一个方法就可以了:

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. - (void) doPrint:(Printer *)printer;  
这里看到了,这个方法的参数类型就是父类的类型,这就是多态,定义类型为父类类型,实际类型为子类类型

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. - (void) doPrint:(Printer *)printer{  
  2.     [printer print];  
  3. }  
这里调用print方法,就是传递进来的实际类型的print方法。

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. Printer *p1 = [[ColorPrinter alloc] init];  
  2. Printer *p2 = [[BlackPrinter alloc] init];  
  3.           
  4. [person doPrint:p1];  
  5. [person doPrint:p2];  
这里的p1,p2表面上的类型是Printer,但是实际类型是子类类型,所以会调用他们自己对应的print方法。


从上面的例子中我们就可以看到多态的特新很是重要,当然也是三大特性中比较难理解的,但是在coding的过程中,用多了就自然理解了,没必要去刻意的理解。


0 0
原创粉丝点击