Extends inheritance operation in Dart learned by Flutter

We have this inheritance operation in other languages, and also exist in dart, using the extends keyword as inheritance. The following is a detailed introduction to inheritance in dart.

Inheritance Operations in Dart

  • Inherit a class using the keyword extends

  • Subclasses inherit the properties and methods visible to the parent class, but not constructors

  • Subclasses can override superclass methods, getters and setters

  • single inheritance, polymorphism

actual combat

Create a new Person.dart  file as the parent class

  1. class Person{
  2. String name;
  3. int age;
  4. // computed property
  5. bool get adult => this.age > 18;
  6. // private property, invisible to subclasses
  7. String _birthday;
  8. void run(){
  9. print("Person running...");
  10. }
  11. }

Subclass accesses properties and methods in parent class

Create a subclass to test inheritance, subclass to access the properties and methods of the parent class

  1. // import person file
  2. import 'Person.dart';
  3. class Student extends Person{
  4. void study(){
  5. print("Student studying...");
  6. }
  7. }
  8. void main (){
  9. Student student = new Student();
  10. // call the subclass's own method
  11. student.study(); // Student studying...
  12. // access properties in parent class
  13. student.age = 20;
  14. // call the method of the parent class
  15. student.run(); // Person running...
  16. // access the computed property of the parent class
  17. print(student.adult); // true
  18. }

Overriding parent class methods and computed properties

Similar to java, use @override to override.

  1. import 'Person.dart';
  2. class Student extends Perosn{
  3. // Override the computed property of the parent class
  4. bool get adult => this.age > 15;
  5. void study(){
  6. print("Student studying...");
  7. }
  8. @override
  9. void run() {
  10. // call the method of the parent class
  11. super.run ( ) ;
  12. print("student running...");
  13. }
  14. }
  15. void main (){
  16. Student student = new Student();
  17. student.age = 16;
  18. student.run(); // Person running... student running...
  19. print(student.adult); // true
  20. }

 

The use of polymorphism in inheritance

  1. // import person file
  2. import 'Person.dart';
  3. class Student extends Person{
  4. void study(){
  5. print("Student studying...");
  6. }
  7. @override
  8. void run() {
  9. // call the method of the parent class
  10. super.run ( ) ;
  11. print("student running...");
  12. }
  13. }
  14. void main (){
  15. Person person = new Student();
  16. // error: The method of the subclass cannot be accessed here, because the object generated using polymorphism is Person, and there is no study method in Person
  17. person.study ( );
  18. // Use is to convert person to Student, which can be accessed below
  19. if (person is Student) {
  20. person.run ( );
  21. person.study ( );
  22. }
  23. }

 

Constructor in inheritance

  • The constructor of the subclass will call the nameless parameterless constructor of the superclass by default

  • If the parent class does not have a nameless and no-argument constructor, you need to explicitly call the constructor of the parent class

  • Used after constructor parameters: Display calling the parent class constructor

first case

  1. void main () {
  2. var student = new Person (); // Person ...
  3. }
  4. class Person{
  5. // The no-argument constructor of the parent class is generally omitted or not written
  6. Person(){
  7. print("Person...");
  8. }
  9. }
  10. class Student extends Person{
  11. }

second case

  1. void main () {
  2. var student = new Student("name"); // object
  3. }
  4. class Person{
  5. String name;
  6. // constructor in parent class
  7. Person(this.name);
  8. Person.withName(this.name){
  9. print("object");
  10. }
  11. }
  12. class Student extends Person{
  13. // use: super to call the constructor of the parent class
  14. // Student(String name) : super(name); This writing is consistent with the following writing
  15. Student(String name) : super.withName(name);
  16. }

Constructor execution order

  • The constructor of the parent class is called at the point where the body of the child class constructor begins to execute

  • If there is an initialization list, the initialization list will be executed before the super class constructor

The constructor initialization list of the subclass must be before the initialization list of the parent class, otherwise an error will be reported.

 

 

Related: Extends inheritance operation in Dart learned by Flutter