OC多态

  1. 面向对象的三大特性: 封装继承与多态;
    封装是对类中的一些字段, 方法进行保护, 不被外界所访问到, 有一种权限控制的功能, OC中有四种访问权限的修饰符; 这样我们在定义类的时候, 那些字段你和方法不想暴漏出去, 那些字段和方法可以暴漏, 可以通过修饰符来完成, 这就是封装!
    @public, @protected, @private, @package
    其默认是@protected;

  2. 多态
    多态对于面向对象编程思想来说, 对以后编写代码的方式一起到很重要的方式, 其实现在很多设计模式中大多数都用到了多态的特性, 多台的定义: 一般基于接口形式实现的!

      Printer.h
      @interface Printer : NSObject
      - (void)print;  // 简单的方法print
      @end;
    
      Printer.m
      #import "Printer.h"
      @implementation Printer
      - (void)print {
      NSLog(@"打印机打印纸张");
      }
      @end
    

创建一个子类,
ColorPrinter.h
#import "printer.h"
@interface ColorPrinter : Printer
- (void)print;
@end

     ColorPrinter.m
     #import "ColorPrinter.h"
     @implementation ColorPrinter
     - (void)print{
     NSLog(@"彩色打印机");
     }
     @end

另一个子类
BlackPrinter.h
#import "Printer.h"
@interface BlackPrinter : Printer
- (void)print;
@end
BlackPringer.m
@implementation BlackPrinter
- (void)print{
NSLog(@"黑白打印机");
}
@end
定义一个Person类来操作具体的打印机
Person.h
#import "ColorPrinter.h"
#import "BlackPrinter.h"
// 扩展性不高, 当我们需要添加一个新的打印机的时候, 还要定义对应的方法
// 所以这时候就可以使用多态
@interface Person : Nsbject {
NSString *_name;
};
// - (void) printWithColor:(ColorPrinter *)colorPrint;
// - (void) printWithBlack:(BlackPriter *)blackPrint;
- (void) doPrint:(Printer *)printer;
@end

     Person.m
     #import "Person.h"
     @implementation Person
     // - (void) printWithColor:(ColorPringter *)colorPrinter{
     [colorPrint print];
     }

     //  - (void) printWithBlack:(BlackPrinter *)blackPrint{
     [blackPrint print];
     }
     - (void) doPrint: (Printer *)printer{
     [printer print];
     }
     @end

测试代码
main.m
#import "Person.h"
#import "BlackPrinter.h"
#import "ColorPrinter.h"

    int main(int argc, const char* argv[]){
    @autoreleasepool {
     Person *person = [[Person alloc] init];
     
     // 多态定义  注意差别
      ColorPrinter *colorPrint = [[ColorPrinter alloc] init];
      BlackPrinter *blackPrint = [[BlackPrinter alloc] init];
     [person doPrint:colorPrinter];
     [person doPrint:blackPrinter]; 

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

[person printWithBlack: blackPrint]; // 调用黑白打印机
[person printWithColor:colorPrint]; // 调用彩色打印机

这种设计就不好! 假设又多了一台打印机! 则需要在Person.h文件中重新声明新的方法, 在.m中去实现! 所以多态的好处就出来了, 使用父类类型, 在Person.h中定义一个方法就可以了!
- (void)doPrint:(Printer *)printer;
这个方法的参数类型是父类的类型, 这就是多态, 定义类型是弗雷类型, 实际类型是子类类型!
- (void) doPrint:(Printer *)printer{ [printer print]; }

调用doprint方法

Printer *p1 = [[ColorPrinter alloc] init];
Printer *p2 = [[BlackPrinter alloc] init];
[person doPrint:p1];
[person doPrint: p2];

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 类有三大特性:继承,封装,多态: 封装 封装就是对类中的一些字段,方法进行保护,不被外界所访问到,有一种权限的控制...
    _Dam0n阅读 956评论 0 0
  • 1. 多态 父类的指针指向子类的指针 不同的子类相应父类的行为 父类:打印机 #import@interface ...
    握青春阅读 474评论 0 0
  • 多态:同一种类型 具有多种的表现形态 多态的条件:必须存在继承关系子类重写父类的方法❗️父类声明的变量指向子类对象...
    J_coco阅读 502评论 0 2
  • OC面向对象—多态 OC面向对象—多态一、基本概念多态在代码中的体现,即为多种形态,必须要有继承,没有继承就没有多...
    清风沐沐阅读 802评论 0 1
  • 还是应该早睡早起,否则真困的很哩。国家,家国,小说中的价值观真的影响到现实的我,穷则独善其身,我很穷而且没有本事,...
    只是孤独阅读 192评论 0 0

友情链接更多精彩内容