Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities

Define a subclass

Inheritance in Dart uses extends to extend a class. Subclasses inherit properties and methods other than the constructor. Dart is single-inherited.

class LGPerson {
  String? name;
  int? age;
  int? _height;
  bool get isFree => _height! < 110;
  run() {
    print('Person run... '); }}class Student extends LGPerson {
  study(){
    print('study... '); }}void extendsDemo(){
  Student st = Student();
  st.age = 19;
  st.run();
  st.study();
}
Copy the code

Overrides the properties of the parent class

The @override keyword is automatically generated when the parent isFree is overridden.

class Student extends LGPerson {
  study(){
    print('study... ');
  }
  @override
  // TODO: implement isFree
  bool get isFree => age! < 18;
}
Copy the code

The polypeptide uses if (st is Student). Since all classes are integrated into Object, LGPerson omits the extends Object. Object has a method external String toString(); The oc-like description can also be overridden to return a custom class description.

void extendsDemo(){
  Student st = Student();
  if (st is Student) {
    st.age = 19;
    st.run();
    st.study();
    print(st.isFree);
    print(st.toString()); /// Similar to description of OC}}Copy the code

When a subclass inherits its parent class, it also calls the default parent constructor

class LGPerson {
  String? name;
  int? age;
  int? _height;
  bool get isFree => _height! < 110;
  run() {
    print('Person run... ');
  }
  /// Default constructor
  LGPerson(){
    print(The 'person constructor.. '); }}void extendsDemo(){
  Student st = Student();/// This calls up the default constructor for the parent LGPerson class
}
Copy the code

Constructor of subclasses

We added a custom constructor to the parent class

class LGPerson {
  String? name;
  int? age;
  int? _height;
  bool get isFree => _height! < 110;
  run() {
    print('Person run... ');
  }
  LGPerson.init();
  LGPerson(this.name);
  LGPerson.withName(this.name);
}
Copy the code

At this point, an error is reported at the instantiation of the subclass Student

Holding option + Enter requires any of these three methods to be implemented

Of course, we can also not implement the parent class, but when creating our own constructor points to the parent class.

class Student extends LGPerson {
  // Student.withName(String? name) : super.withName(name); Constructor of the parent class
  // Student(String? name) : super(name); Constructor of the parent class
  // Student.init() : super.init(); Constructor of the parent class
   Student(): super.init(); /// Custom constructors This is written like an initializer list

  study(){
    print('study... ');
  }
  @override
  // TODO: implement isFree
  bool get isFree => age! < 18;

  @override
  run() {
    // TODO: implement run
    print('student run... '); }}Copy the code

Student(): super.init(); This is written somewhat like the initializer list described earlier. Can this syntax be shared with initializers? The answer is yes. The purpose of the initializer list is to assign values to final variables and validate passed parameters.

class Student extends LGPerson {
  final String subName;
   Student.withName(String?name) : subName = name! .super.withName(name);
  // Student(String? name) : super(name);
  // Student.init() : super.init();
   Student(): subName = 'hello'.super.init();
}
Copy the code

Note that the assignment of subName must precede the constructor.