Dart: abstract class, interface related knowledge collation


foreword

This article introduces the knowledge points related to abstract classes and interfaces in Dart

1. Abstract class

An abstract class is a template, the specific meaning is as follows

Meaning: Mainly used for constraints, definition standards, subclasses can inherit abstract classes, and can also implement abstract class interfaces.

In Dart, the relevant restrictions on abstract classes are as follows

  • Abstract classes are defined with the abstract keyword
  • Abstract methods in dart cannot be declared using abstrac, and methods without method bodies in dart are called abstract methods
  • If the subclass inherits the abstract class, it must implement the abstract method in the abstract class
  • If the abstract class is implemented as an interface, all properties and methods defined in the abstract class must be implemented
  • An abstract class cannot be instantiated, only subclasses that inherit from it can

The difference between extends and Implements keywords

  • If you reuse the methods in the abstract class, you must use the abstract method to constrain the subclass, then use extend to inherit the abstract class
  • If we just regard abstract classes as standard, we use implements to implement abstract classes

For example, in the following case, the subclass of Animal does not need to implement all the abstract methods of its parent class

abstract class Animal {
    
      // 抽象类
eat(); // 抽象方法
run();
}
class Dog extends Animal {
    
    

eat() {
    
    
print( "小狗吃骨头" )
}

run() {
    
    
print( "小狗在跑" )
}
}
class Cat extends Animal {
    
    

eat() {
    
    

}
}
main() {
    
    
Dog d = new Dog();
d.eat();
Cat c = new Cat();
c.eat();
}

2. Interface

First of all, the interface of dart does not have the interface keyword to define the interface, but ordinary classes or abstract classes can be implemented as interfaces, and the
implements keyword is also used for implementation.
If the implemented class is an ordinary class, all the attributes and methods in the ordinary class and the abstract will be overwritten.
Because abstract classes can define abstract methods, ordinary classes cannot, it is recommended to use abstract classes to define interfaces

Demo code is as follows

	abstract class Db {
    
    
  String url; // 数据库的链接地址
  add(String data);
  save();
  delete();
}
class Mysql implements Db {
    
    
  
  add(data) {
    
    
   print('这是mysql的add方法' + data);
  }

  
  delete() {
    
    
    return null;
  }

  
  save() {
    
    
    return null;
  }

  
  String url;

  Mysql(this.url);

}
void main() {
    
    
  Mysql mysql = new Mysql('fegggergre');
  mysql.add("哈哈哈");
}

In Dart, there is single inheritance, but multiple interfaces can be implemented, as follows

class C implements A,B {
    
    } // 一个类实现多个接口,那么就必须这个类实现所有接口的方法与属性

Three, mixins

Dart has the mixins feature, as follows

  • Mixins can be used to implement multiple inheritance functions
  • Conditions for using mixins
  • Classes as mixins can only inherit object, and cannot inherit other classes (a and b below cannot inherit other classes)
  • Classes used as mixins cannot have constructors (a and b below cannot have constructors)
  • A class can mixins multiple mixins classes
  • Mixins are by no means inheritance, nor interface, but a brand new feature
class A {
    
    
void printA(){
    
    
print("A")
}
}
class B {
    
    
void printB(){
    
    
print("B")
}
}
class C with A,B {
    
    

}
main() {
    
    
C  c = new C()
c.printB()
c.printA()
}

Guess you like

Origin blog.csdn.net/qjyws/article/details/128819341