美文网首页flutter
11.2 Dart中多态

11.2 Dart中多态

作者: __Mr_Xie__ | 来源:发表于2023-03-15 08:56 被阅读0次

Datr中的多态:

允许将子类类型的指针赋值给父类类型的指针, 同一个函数调用会有不同的执行效果 。

子类的实例赋值给父类的引用。

多态就是父类定义一个方法不去实现,让继承他的子类去实现,每个子类有不同的表现。

abstract class Animal{
  eat();   //抽象方法 
}

class Dog extends Animal{
  @override
  eat() {
     print('小狗在吃骨头');
  }
  run(){
    print('run');
  }
}

class Cat extends Animal{
  @override
  eat() {   
    print('小猫在吃老鼠');
  }
  run(){
    print('run');
  }
}

main(){
  // Dog d=new Dog();
  // d.eat();
  // d.run();

  // Cat c=new Cat();
  // c.eat();

  Animal d=new Dog();

  d.eat();

  Animal c=new Cat();

  c.eat();
}

相关文章

网友评论

    本文标题:11.2 Dart中多态

    本文链接:https://www.haomeiwen.com/subject/xneykdtx.html