Xiao CAI in the process of simple learning source code often encounter mixin type of Class Class, and xiao CAI before is to do Android development, Java/Kotlin and there is no mixin concept, xiao CAI today a simple understanding;

Mixin

Basic introduction

Mixins are a way to reuse class code in multiple class hierarchies; Xiao CAI consulted a lot of information, the official introduction is:

Mixins are classes in object-oriented programming languages that provide implementations of methods that other classes can access without subclassing. Mixins provide additional functionality to the Class classes that use them, but they are not used in isolation (they cannot generate instance objects separately and are abstract classes). Mixin classes are often used as functional modules that are “mixed in” when needed and do not complicate Class relationships. Mixin is conducive to code reuse and avoids the complexity of multiple inheritance. Mixin enjoys the simplicity of single inheritance and the co-existence of multiple inheritance. The interface interface is similar to Mixin in that it can be multiple inheritance, but the difference is that Mixin can be realized.

Corresponding relations between

inheritance Mixed with interface
The keyword extends with
Corresponding to the number 1:1 1:n
Code setup sequence before In the

Case try

Xiao CAI tries the simplest case to learn Mixin step by step; Xiao CAI defines three common classes, People/Teacher/Student, to achieve a basic speak() method;

class People { void speak() { print('People --> speak'); } } class Student { void speak() { print('Student --> speak'); } } class Teacher { void speak() { print('Teacher --> speak'); } } // mixin Teacher { void speak() { print('Teacher --> speak'); }}Copy the code

1. Extends to inherit

class People01 extends Student { }

class People02 extends Teacher { }

class People03 extends Teacher { void speak() { print('People03 --> speak'); } }

print('<----------- People01 ----------->');
People01 people01 = People01();    people01.speak();
print('<----------- People02 ----------->');
People02 people02 = People02();    people02.speak();
print('<----------- People03 ----------->');
People03 people03 = People03();    people03.speak();
Copy the code

The extends keyword is used when using inheritance. Dart can only implement a single inheritance, and subclasses override functions of the same name as their parent class.

2. With the Mixin

class People04 with Student { }

class People05 with Teacher { }

class People06 with Teacher { void speak() { print('People06 --> speak'); } }

class People07 with Student, Teacher {}

class People08 with Teacher, Student {}

print('<----------- People04 ----------->');
People04 people04 = People04();    people04.speak();
print('<----------- People05 ----------->');
People05 people05 = People05();    people05.speak();
print('<----------- People06 ----------->');
People06 people06 = People06();    people06.speak();
print('<----------- People07 ----------->');
People07 people07 = People07();    people07.speak();
print('<----------- People08 ----------->');
People08 people08 = People08();    people08.speak();
Copy the code

  1. The with keyword is used when Mixin is used.
  2. Subclasses override the same functions that are mixed into the class;
  3. Subclasses can be mixed with more than one class;
  4. Subclasses mixed with more than one class is related to the declaration order;

Simple to understand, subclass functions to implementwithThe following statement shall prevailPeople07For example,People07Priority implementationStudentIn thespeak(); And then implement itTeacherIn thespeak()Function to coverStudentClassspeak()Functions; ifPeople07Subclasses also have functions of the same name that continue to be overwrittenTeacherClassspeak()Functions;

3. Extends extends + with with mixins

class People09 extends Student with Teacher {}

class People10 extends Teacher with Student {}

class People11 extends Student with Teacher, People {}

class People12 extends Student with Teacher, People { void speak() { print('People12 --> speak'); } }

print('<----------- People09 ----------->');
People09 people09 = People09();    people09.speak();
print('<----------- People10 ----------->');
People10 people10 = People10();    people10.speak();
print('<----------- People11 ----------->');
People11 people11 = People11();    people11.speak();
print('<----------- People12 ----------->');
People12 people12 = People12();    people12.speak();
Copy the code

Side dishes to tryextendsInheritance andwithWhen mixed with concurrent applications, the final result of subclasses is related to the declaration order mentioned above; Overall order of execution:extendsInheritance takes precedence, followed bywithMixin, followed by subclass function of the same name overwrite;

4. Implements the interface

class People13 implements People { @override void speak() { // TODO: implement speak print('People13 --> speak'); }}Copy the code

Β Β Β Β Β Β Dart 与 JavaDifferent, nointerfaceInterface, butDartEach class has an implicit interface that contains all of the class’s member variables and defined methods. Subclasses can implement this implicit interface; When the parent class is used as an implicit interface, the subclass needs to overload@overrideMethods of the parent class;

5. Implements interface + with Mixin

class People14 with Student implements People { @override void speak() { // TODO: implement speak super.speak(); print('People14 --> speak'); } } class People15 with Student, Teacher implements People { @override void speak() { // TODO: implement speak super.speak(); print('People15 --> speak'); }}Copy the code

Side dishes to trywithWith andimplementsWhen the interface is applied at the same time, the method of the parent class still needs to be overridden. But you can get through nowsuper.speak()usewithMixed function methods;

6. Extends extends with Mixin + implements

mixin Doctor { void speak(); } class People16 extends People with Student, Teacher implements Doctor { @override void speak() { // TODO: implement speak super.speak(); print('People16 --> speak'); }}Copy the code

Side dishes to tryextends / with / implementsWhen the three are applied simultaneously, the order of execution isextendsInheritance takes precedence, followed bywithInterfuse, and finallyimplementsInterface overload;

Matters needing attention:

  1. A class cannot be used as both an inheritance and an interface;
// error class People extends People implements People {}Copy the code
  1. When mixing with a Mixin class, the parent class cannot contain constructors;
Class Teacher {Teacher(); void speak() { print('Teacher --> speak'); Class People with Teacher {} class People with Teacher {}Copy the code
  1. A class with mixin can be modified with mixin as an abstract mixin method that cannot be inherited by extends but can be used as an implicit interface.
// mixin Teacher {void speak() {print('Teacher --> speak'); }} class People extends Teacher {}Copy the code
  1. On can be used for mixin-modified classes, similar to inherited parent classes;
class People { void speak() { print('People --> speak'); } } class Student extends People { void speak() { print('Student --> speak'); } } mixin Teacher on People { void speak() { print('Teacher --> speak'); }}Copy the code

Mixin can be used as a way of thinking about multiple inheritance, just through the non-inheritance way to reuse the function code in the class; Xiao CAI’s understanding of Mixin is not deep enough, if there are mistakes, please give more guidance!

Source: Little Monk A Ce