Abstract class polymorphism and interface in Dart

Abstract classes in Dart : Drat abstract classes are mainly used to define standards, subclasses can inherit abstract classes, and can also implement abstract class interfaces.
1. Abstract classes are defined through the abstract keyword.
2. Abstract methods in Drat cannot be declared with abstract. Methods without method bodies in Dart are called abstract methods.
3. If the subclass inherits the abstract class, it must implement the abstract method in it.
4. If the abstract class is implemented as an interface, it must implement all the properties and methods defined in the abstract class.
5. The abstract class cannot be instantiated, only the subclasses that inherit it can

The difference between the extends abstract class and implements:
1. If you want to reuse the methods in the abstract class and use the abstract method to constrain the subclasses, we use extends to inherit the abstract class. 2.
If you just use the abstract class as a standard, we use implements to implement the abstract class

Case: Defining an Animal class requires that its subclasses must include the eat method.
The role of abstract classes is mainly to constrain definition standards. If there are abstract methods in an abstract class, the subclass must inherit the abstract class and must rewrite its abstract method.

abstract class Animal {
  eat(); //抽象方法 在抽象类里面定义的方法在子类里必须实现,如果不在子类里重写的话子类会报错
  run();
  printInfo() {
    print('我是一个抽象类里面的普通方法');
  }
}

class Dog extends Animal {
  @override
  eat() {
    print('小狗eat');
  }

  @override
  run() {
    print('小狗run');
  }
}

main() {
  Dog d = new Dog();
  d.eat(); //小狗eat
  d.printInfo(); //我是一个抽象类里面的普通方法
}

Polymorphism in Dart : It is allowed to assign a pointer of subclass type to a pointer of parent class type. The same function call will have different execution effects. The
instance of subclass is assigned to the reference of the parent class.
Polymorphism means that the parent class defines a method and does not implement it. Let the subclasses inheriting it implement it. Each subclass has different performance.

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

class Dog extends Animal {
  @override
  eat() {
    print('小狗eat');
  }
}

class Cat extends Animal {
  @override
  eat() {
    print('小猫eat');
  }
}

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

  Cat c = new Cat();
  c.eat(); //小猫eat
}

Interface :
Like Java, dart also has an interface, but it is still different from Java.
First of all, dart's interface does not have the interface keyword to define the interface, but ordinary classes or abstract classes can be implemented as interfaces. The same is done
using keywords. But the interface of dart is a bit strange. If the implemented class is an ordinary class, all the methods of the ordinary class and the attributes in the abstract will need to be overwritten. And because abstract classes can define abstract methods, ordinary classes cannot, so generally if you want to implement a method like a Java interface, you will generally use abstract classes. It is recommended to define interfaces using abstract classes.implements


//例子:定义一个DB库 支持mysql mssql mongdb
//mysql mssql mongdb这三个类里面都有同样的方法

abstract class Db {
  // 当作接口 接口:就是约定 规范
  late String uri; //数据库的链接地址
  add(String data);
  save();
  delete();
}

class Mysql implements Db {
  @override
  late String uri;

  Mysql(this.uri);

  @override
  add(data) {
    print('这是mysql的add方法' + data);
  }

  @override
  delete() {
    // TODO: implement delete
    throw UnimplementedError();
  }

  @override
  save() {
    // TODO: implement save
    throw UnimplementedError();
  }
}

class Mssql implements Db {
  @override
  late String uri;

  @override
  add(String data) {
    print('这是MssqlMssql的add方法' + data);
  }

  @override
  delete() {
    // TODO: implement delete
    throw UnimplementedError();
  }

  @override
  save() {
    // TODO: implement save
    throw UnimplementedError();
  }
}

main() {
  //xxxxxxx-- 数据库地址
  Mysql mysql = new Mysql('xxxxxxx');
  mysql.add('12345');
}

mixinsThe Chinese meaning is to mix in, which is to mix other functions into the class.

In Dart, mixins can be used to implement functions similar to multiple inheritance

Because the conditions for using mixins have been
changing with the Dart version, here are the conditions for using mixins in Dart2 .


class A {
  String info = 'this is A';
  void printA() {
    print("A");
  }
}

class B {
  void printB() {
    print("B");
  }
}

class C with A, B {}

void main() {
  var c = new C();
  c.printA(); //A
  c.printB(); //B
  print(c.info); //this is A
}

Guess you like

Origin blog.csdn.net/m0_48259951/article/details/129315780