Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

This article also participated in the “Digitalstar Project” to win a creative gift package and creative incentive money.

📝 [Flutter] learning to form a record, [programmer essential knowledge]

📔 Today’s small knowledge — Dart Mixins in Flutter!

1. Write at the front

After introducing abstract classes and implements in Dart in the previous article, let’s move on to learning about Mixins in Dart.

The Apple Mac is configured with the Flutter development environment

Android Studio installs a third-party simulator for Flutter — netease MuMu

Failed to find Build Tools Revision 29.0.2

Android license status unknown. Run ‘Flutter doctor — Android – Licenses’

How to create a Flutter project and run your first Flutter project

Dart uses var, final, and const

Dart Indicates the num of the data type

Dart String of data type

Dart data type list&Map

Dart method and arrow function

Dart’s method passes optional parameters and methods as parameters

Dart, Dart, Dart, Dart

Dart classes and objects in Flutter

Dart constructor of Flutter

Dart factory constructor & singleton & Initializer list

Dart class methods and object operators for Flutter

Inheritance of Flutter Dart

Dart abstract classes and interfaces in Flutter

2. What is mixin?

What are Mixins? In plain English, multiple inheritance

2.1 Mixing Examples

  • with
class D1 extends A1 with B1.C1 {
  d1() => print("D1...");

}
class A1 {
  a1() => print("A1...");
}
class B1 {
  b1() => print("B1...");
}
class C1 {
  c1() => print("C1...");
}
Copy the code

D1 extends extends A1 with B1 and C1.

  • The mixed objects are classes
  • You can mix multiple
void main(a) {
  mixinTest();
}
mixinTest(){
  D1 d = D1();
  d.d1();
  d.a1();
  d.b1();
  d.c1();
}
Copy the code
  • The print result is as follows:

Mixing multiple classes, which brings great convenience to method reuse, removes many restrictions, there is no need to instantiate mixed classes, and there is no need to mix class singleton.

2.2 What happens when methods with the same name are mixed?

If multiple classes with the same name call this method, whose call is it?

class D1 extends A1 with B1.C1 {
  d1() => print("D1...");

}
class A1 {
  a1() => print("A1...");
}
class B1 {
  a1() => print("B1...");
}
class C1 {
  a1() => print("C1...");
}
Copy the code
  • The test results

From the printed result, if there is a method with the same name in the mixed class, it is the last one mixed, who calls the method, the first one will be overwritten by the back!

  • As mixin classes, constructors cannot be implemented

As a mixin class, the constructor cannot be implemented and an error is reported as follows:

2.3 Mixed abbreviations

  • We could also write interfuse like this
class D1 = A1 with B1,C1;
// class D1 extends A1 with B1,C1 {
//
// }
Copy the code

This is easy to write as long as there are no properties and methods in D1.

2. 4 Operator Overloading

  • Operator overloading

In normal cases, objects are not comparable and an error is reported as follows:

  • Overloading operator code
operatorTest(){
  OperatorClass op1 = OperatorClass(20);
  OperatorClass op2 = OperatorClass(18);
  // It is not possible to compare objects in normal cases, so you need to overload the operator
  print(op1 > op2);

}
class OperatorClass {
  int age;
  OperatorClass(this.age);
  // Override the operator
  bool operator > (OperatorClass other) => this.age > other.age;
}
Copy the code

3. Write in the back

Follow me, more content continues to output

  • CSDN
  • The Denver nuggets
  • Jane’s book

🌹 if you like, give it a thumbs up 👍🌹

🌹 feel harvest, can come to a wave of collection + attention, so as not to find you next time I 😁🌹

🌹 welcome everyone to leave a message exchange, criticism and correction, forwarding please indicate the source, thank you for your support! 🌹