I. Overview
- Inheritance (keyword extends)
- Mixed mixins (keywords with)
- Interface (keyword implements)
These three relationships may exist, but there are before and after the sequence:
extends -> mixins -> implements
extens front, as mixins in the middle, the implements and finally
Second, inheritance (extends)
Flutter in succession and inheritance in Java is the same:
Flutter Inheritance is single inheritance
- (1) subclass use extends keyword to inherit the parent class
- (2) sub-class parent class inherit properties and methods visible but not inherit constructors
- Method (3) capable of replication subclasses of the parent class getter and setter
- Method (4) subclass overrides the superclass, use @override
- (5) a subclass method calls the superclass, use super
- (6) subclasses can inherit the parent class of non-private variables
see the following code to deepen understanding:
class the Person {
// variables public String name; NUM Age;
// private variables String's _gender; // constructor class name the Person ( the this .name, the this .age);
// public methods void printInfo () { Print ( " {} --- $ this.name $ this.age {} " ); }
work () { Print ( " $ {} this.name at work ... " ); } } class Web the extends the Person {
Web (String name, Age NUM): Super (name, Age);
RUN () { Print ( ' RUN ' ); super.work (); // parent class from class to call }
// Methods override the parent class @override // can write you can not write recommend plus override the parent class method when @override void printInfo ( ) { Print ( " name: $ {this.name} --- Age: this.age $ {} " ); } } main () { the Web W = new new the Web ( ' John Doe ' , 20 is ); // W .printInfo (); w.run (); }
Second, mixed mixins (with)
mixins
in Chinese means
mixing
, is in a class
mixed with
other functions. Dart can be used in
mixins
achieve a similar function as multiple inheritance of
mixins
conditions of use, along with Dart version has been changed, I said here is Dart2.x use
mixins
conditions:
- (1) as mixins class can only inherit from Object, you can not inherit from other classes
- (2) as mixins classes can not have a constructor
- (3) a class can mixins plurality mixins classes
- (4) mixins not inherited, nor is the interface, but a new feature
to see specific code:
class Person{ String name; num age; Person(this.name,this.age); printInfo(){ print('${this.name}----${this.age}'); } void run(){ print("Person Run"); } } class A { String info="this is A"; void printA(){ print("A"); } void run(){ print("A Run"); } } class B { void printB(){ print("B"); } void run(){ print("B Run"); } } class C extends Person with B,A{ C(String name, num age) : super(name, age); } void main(){ var c=new C('张三',20); c.printInfo(); // c.printB(); // print(c.info); c.run(); }
3. interface (implements)
Flutter is no interface, but Flutter Each class is an implicit interface that contains all the class member variables and methods defined. If there is a class A, class B you want to have the API A, but do not want to have achieved in the A, then you should put as interfaces A, Class A. Class B implements
it in the Flutter: class is interface
- When the class is used as the interface method is a method in the class interface, you need to re-implement the sub-class, sub-class implementation when you want to add @override
- When the class is used as the interface, the class member variables also need to be implemented in the subclass. In the former member variables plus @override
/ * Dart class implements a plurality of interfaces: * / abstract class A { String name; Printa (); } abstract class B { printB (); } class C the implements A, B { @override String name; @override Printa ( ) { Print ( ' Printa ' ); } @override printB () { // the TODO: Implement printB return null ; } } void main () { C C = new new C (); c.printA (); }
Reproduced in: https: //www.cnblogs.com/lxlx1798/p/11044101.html