I. Key words

  • Component unit: Common class, abstract class
  • Connection relationship: implements, extends, mixin

Dart has no interface compared to Java, adding mixins

Two, component unit

2.1 ordinary class

You can create new instances that are extends, implements, and mixin.

2.2 Abstract class-abstract

Abstract abstract is usually used to describe a parent class that has a behavior but does not have a detailed implementation, and requires subclasses to implement the abstract details. In this case, the parent class is defined as an abstract class, and the subclass inherits the parent class and implements its abstract methods.

  • Abstract classes are implemented using abstract, but abstract functions do not need to use abstract, and can be defined directly without method body implementation.
  • You can have data in an abstract class, you can have general functions, you can have abstract functions, but an abstract class cannot be instantiated. When a subclass inherits an abstract class, it must implement its abstract functions.
Void main() {// Abstract classes can't be created with a 'new' expresson // var p = new Person(); var t = new Teacher(); t.printName(); var s = new Student(); s.printName(); } abstract class Person { static const String name = "akironer"; void printName() { print(name); Class Student implements Person {void printName() {class Student implements Person {class Student implements Person {void printName() { print('akironer2'); }}Copy the code

Third, relationship connection

3.1 Inheritance – extend

The Dart language bills itself as “single inheritance,” meaning that a class can have only one immediate parent. If a class does not explicitly declare a parent class, it inherits the Object class by default. In addition, the Dart language provides a Mixin syntax that allows subclasses to mix with other classes while inheriting from their parent.

  • Subclasses can inherit properties and methods visible from their parent class. In Java, visibility means non-private decoration; For Dart, it means a non-underscore _ beginning.
  • The subclass calls the methods of the parent class, using the super keyword. A subclass overrides a method of its parent class, using the override keyword
  • Subclasses do not inherit the constructor of their parent class.

3.2 Implementation – implements

Dart does not have the interface keyword, but implements the keyword. Dart allows you to use classes as implicit interfaces. If an interface is required, you can declare a class instead.

void main() { var dog = new Dog(); dog.eat(); dog.swim(); dog.walk(); } abstract class Animal{ static const String name = 'AA'; Void display(){print(" name = ${name}"); } void eat(); Abstract Class Swimable {void swim(); } class Dog implements Animal implements swimable {void walk(){}} walkable { @override void eat() { print("eat"); } @override void swim() { print("swim"); } @override void walk() { print("walk"); }}Copy the code

3.2 Mix-in

Mixins are generally used to describe a block that has a particular function, and an object can have multiple blocks with different functions. Mixins are used to modify classes. Similar to abstract, a class can have member variables, ordinary methods, abstract methods, but cannot be instantiated.

  • Mixins are not a way to get multiple inheritance in the classical sense.
  • Mixins are a way to abstract and reuse a series of operations and states.
  • It is similar to the reuse achieved by extending classes, but it is compatible with single inheritance because it is linear.

3.2.1 Simple Applications

The simplest mixin consists of the mixin & with keyword.

One faculty is to draw.

void main() { Teacher().draw(); } mixin DrawFunc { String content = '.. '; String what(); void draw() { print('I can draw ${what()}'); } } class Teacher with DrawFunc { String what() => "car"; }Copy the code

3.2.2 Defining types (mixin… on)

When we use the on keyword on mixins, mixins can only be used on subclasses of that class, and mixins can call methods of that class.

The ability to draw can only be applied to humans

void main() { Teacher().draw(); } class Person {} mixin DrawFunc on Person { String content = '.. '; String what(); void draw() { print('I can draw ${what()}'); } } class Teacher extends Person with DrawFunc { String what() => "car"; }Copy the code

3.2.3 Multiple types

On the basis of ‘painting’, we added a new ability ‘singing’.

void main() { Teacher().draw(); Teacher().sing(); } class Person {} mixin DrawFunc on Person { String content = '.. '; String what(); void draw() { print('I can draw ${what()}'); } } mixin SingFunc on Person { void sing() { print('I can sing'); } } class Teacher extends Person with DrawFunc, SingFunc { String what() => "car"; }Copy the code

3.2.4 Combining blocks

  • mixin: blocks are defined.
  • on: Specifies the usemixinThe host of a block must inherit from a particular class; inmixinTo access the members and methods of that particular class.
  • with: Is responsible for composing blocks, whilewithLater, this should be noted, for example:
void main() { Teacher().draw(); Teacher().sing(); Teacher().dance(); } class Person {} mixin DrawFunc on Person { String content = '.. '; String what(); void draw() { print('I can draw ${what()}'); } } mixin SingFunc on Person { void sing() { print('I can sing'); } } abstract class DanceFunc { void dance() { print('I can dance'); } } class Teacher extends Person with DrawFunc, SingFunc, DanceFunc { String what() => "car"; }Copy the code

Fourth, summary.

  • mixinThink of it as blocks of functionality: which hosts need which functionalitywithTo go up.
  • onOn the one hand, keywords are used to limit the application scenarios of blocks, and they can also provide common basic functionality for multiple blocks.

The resources

Flutter Tips – implements、extends、mixin

Knowledge of Flutter combing (Dart) – implements, extends, mixin understanding