The purpose of this blog post is to summarize several types of extensions in the Dart language. This article can only be regarded as a note and a summary.


Inheritance (extends)


When it comes to class extension, you have to talk about inheritance, which is implemented in Dart using the extends keyword.

Dart inheritance characteristics
Inheritance in Dart is single inheritance.
Constructors or destructors cannot be inherited.
A subclass overrides a superclass method by preceded by @override.
The subclass calls the method of the parent class with super.
In Dart, subclasses have access to all variables and methods of their parent class.

Dart subclasses can access all variables and methods of their parent class because Flutter does not have a public or private function. .

Extends is shown in the following example.

class Person {
  String _name;

  void test() {
    _name = "Human";
    print(_name); }}class Man extends Person {
  @override
  void test() {
    super.test();
    _name = "Man"; }}Copy the code


Mixins (with)


Mixins are a new way to extend classes for iOS/Android developers today. Mixins allow methods or properties from one class to be mixed into another class for use.

As an aside, some people say that mixins originated in Smalltalk.Smalltalk may be a strange language, but OC messaging and Java garbage collection are based on Smalltalk (90% validation).

In Flutter, the keywords for mixing mixins are implemented using with. The following is an example.

class Man with RunMan {
  void test() {
    run();// Call the mixin method}}class RunMan {
  void run() {
    print("Running."); }}Copy the code


The languages that now support mixins patterns are listed below.

  • ColdFusion (Class based using includes and Object based by assigning methods from one object to another at runtime)
  • Curl(with Curl RTE)
  • D (called “template mixins”)
  • Factor[citation needed]
  • Fantom
  • Ioke
  • JavaFX Script
  • JavaScript
  • Object REXX
  • OpenLaszlo
  • Perl[3]
  • PLT Scheme (mixins documentation)
  • Python
  • Ruby
  • Scala
  • Smalltalk
  • Strongtalk
  • Vala
  • Visual Dataflex
  • XOTcl/TclOO (object systems for Tcl)[4]


Interface and Implementation (implements)


In Objective-C, the concept of an Interface is the Protocol. In Java, the concept of an Interface is the Interface. Defining an interface in both languages simply declares interface methods. There is no special interface keyword in Flutter. Any Class can be an interface.

Dart interface features
Class is an interface.
A class can implement multiple interfaces simultaneously.
To implement an interface, all methods and member variables in the interface must be implemented.
When class is used as an interface, the methods of class are interface methods. You need to re-implement the interface method using the @override keyword before the method.
When a class is used as an interface, its member variables also need to be reimplemented in the subclass, using the @override keyword.

For details, see the following.

class Man implements Day {
  @override
  String workName;

  @override
  void work() {
    print("At work"); }}class Day {
  String workName;

  void work() {}
}
Copy the code


Extension (extension)


In Dart 2.7, Dart has added an important syntax feature called Extension. Add new member functions to existing classes and add new functions to them. Extension allows you to add handy functions to common classes such as Iterable and String.

You can have multiple extension extensions for a class, but methods in extension extensions cannot have the same name; otherwise, a crash will occur.

Of course, we have other uses for Flutter components, for example. We can’t change the code in the component dependencies library, so we can extend the classes in the dependencies library in this form.

If an extension is defined in another file, you need to import the extension file to use it. For details, see the following.

class Man {
  void test() {
    String string = "1"; string.parseInt(); }}extension ParseNumbers on String {
  int parseInt() {
    return int.parse(this);
  }
  double parseDouble() {
    return double.parse(this); }}Copy the code


Problem: Execution order of methods with the same name


If a method with the same name appears among these types of extensions, what is the execution priority? The summary is as follows.

Execution priority: This class > Mixins > Extends > Extension Extension

If multiple classes after with have the same method, which class’s method will be called when the method is called? Since classes further away from the with keyword override the same method in the previous class, the conclusion is shown below.

  • If the method is overridden using the current class, the method in the current class is called.

  • If the method has not been overridden by the currently in use class, the method in the class farthest from the with keyword is called.

The verification process can be completed by using the following example.

void main() {
  runApp(MyApp());
  print("------main start--------");
  D d = new D();
  d.a();
  d.b();
  d.c();
  d.d();
  print("------main end--------");
}

class A {
  a() {
    print("A.a()");
  }
  d() {
    print("A.d()"); }}class B {
  a() {
    print("B.a()");
  }

  b() {
    print("B.b()");
  }

  d() {
    print("B.d()"); }}class C {
  a() {
    print("C.a()");
  }

  b() {
    print("C.b()");
  }

  c() {
    print("C.c()");
  }

  d() {
    print("C.d()"); }}class D extends C with B.A {
  d() {
    print("d.d()"); }}extension DExtension on D {
  d() {
    print("DExtension.d()"); }}Copy the code


conclusion


OK, this article Flutter related keywords are introduced here, SAO Dong is also from the Internet to organize this article, can be regarded as a summary of their own. Thank you for watching.

Welcome to continue to pay attention to SAO Dong, have any questions welcome to contact SAO Dong.