Abstract classes and methods in Dart
1.1 Defining abstract classes
Abstract classes in DART are primarily used to define standards, and subclasses can inherit abstract classes or implement their interfaces
Void main(){var cat = new A(); var cat = new A(); }Copy the code
1.2 Define abstract methods
Abstract class Person{// Abstract method // abstract method cannot use abstract declaration // void eat(); } // Can't define abstract methods in non-abstract classes class Man{// attribute name = "small "; Void getInfo(){print(" I am ${name}"); } // Void sayHello(); / / error}Copy the code
1.3 Abstract classes can also have non-abstract methods
Abstract classes can also define non-abstract methods that can be called by subclasses
Abstract class Person{// Abstract method eat(); PrintInfo (){print(" I am a non-abstract method in an abstract class "); }} class Man extends Person{String name; Override eat() {print("${this.name} likes to eat "); }} class Child extends Person{String name; Override eat() {print("${this.name} likes to write code "); }} void main(){Man Man = new Man("小美"); Man.eat(); // Call man.printinfo (); // call man.printinfo (); Child Child = new Child(" "); child.eat(); PrintInfo (); printInfo(); // I am a non-abstract method in an abstract class.Copy the code
1.4 Description of Abstract Classes
A subclass must implement all properties and methods defined in the abstract class if the abstract class is implemented as an interface. Abstract classes cannot be instantiated, only subclasses that inherit from them can. 4. Abstract classes are usually used to define interface 5. Abstract classes usually have abstract methods. 6. Classes with abstract methods must be declared as abstract classes.
// define an abstract class. Eat (); } class Dog extends Animal{String name; Override eat() {print("${this.name} likes meat "); }} class Cat extends Animal{String name; Override eat() {print("${this.name} likes fish "); }} void main(){Dog Dog = new Dog(" 一 hA "); dog.eat(); Cat Cat = new Cat(" Cat "); cat.eat(); Persian cats like to eat fish}Copy the code
Second, the polymorphism
Polymorphism is when a parent class defines a method and does not implement it, but lets its subclasses implement it, each of which behaves differently (the parent class is abstract and has abstract methods).
// define an abstract class. Eat (); } class Dog extends Animal{String name; Override eat() {print("${this.name} likes eating bones "); } // Subclass run(){print("${this.name} in happy run "); }} void main(){// Define instance by subclass // Dog Dog = new Dog(" husky "); // dog.eat(); // Dog.run (); // Dog.run (); Animal dog = new dog (" husky "); Animal dog = new dog (" husky ") dog.eat(); // Dog.run (); // Dog.run (); // This is an error}Copy the code
Three, interfaces,
3.1 implements
A class implements one or more interfaces by using the keyword implements.
2. A common class implements an interface, overwriting each member of an interface class (common or abstract). 3. If you reuse an interface from an existing class, use extends 4. If you only use external behavior of an existing class, use interface (implements) 5. Each class implicitly defines an interface containing all instance members
// Use the class keyword to define the ordinary class Person{// Attribute String name; // Constructor Person(this.name); Void sayHello(){print(" hello, my name is ${name}"); }} class Student extends Person{String name; Student(this.name):super(name); Void getInfo(){print(" it's called ${name}"); }} void main(){Student xm = new Student("小 小 "); xm.sayHello(); Xm.getinfo (); xm.getInfo(); // class Student implements Person{// class Student implements Person; Student(this.name); Override void sayHello(){print(${name}); Void getInfo(){print(" he is called ${name}"); }} void main(){Student xm = new Student(" xm "); xm.sayHello(); Xm.getinfo (); } // We'll find out if a subclass inherits its superclass. The superclass defines the interface, and the subclass that implements the interface overwrites all properties and methods in the interface.Copy the code
3.2 Abstract class interface
Use abstract classes as interfaces (classes are interfaces) and let subclasses implement them (the keyword implements). All methods of an abstract class need not define a method body, while methods defined by ordinary classes need to implement a method body. These methods need to be overridden by subclasses of the implementation interface when they are used as interfaces. Therefore, it is not important to use abstract classes as interfaces
// Define an abstract class interface abstract class Db{String URI; // Abstract method add(String data); save(); delete(); } class Mysql extends Db{@override String URI; Mysql(this.uri); Override add(data){print(" this is Mysql ${data}"); } @override save(){print(" this is Mysql save method "); } @override delete(){print(" this is Mysql delete method "); } } void main(){ Mysql mysql = new Mysql("mysql"); mysql.add("123456"); // This is Mysql 123456 mysql.save(); Mysql.delete (); // This is the Mysql delete method}Copy the code
3.3 Multiple Interfaces
Extends extends does not extend to multiple classes. If the extends extends extends is not implemented if you want to extend multiple classes, you can implement multiple interfaces using the implements keyword
Implements implements all methods in a parent class, no matter how many parent interfaces are implemented. Implements implements all methods in a parent class
Abstract class A{String name; // abstract method printA(); } // abstract class B interface abstract class B{int age; // Abstract method printB(); } // class Student implements A,B{class Student implements A,B; int age; Student(this.name,this.age); @override void printA(){print(" printA "); } @override void printB(){print(" printB "); Void sayHello(){print(${name}, ${age}); Void main(){class Student xm = new Student(" xm ",8); xm.printA(); Xm.printb (); Xm.sayhello (); // Hello everyone, my name is Xiaogang and I am 8 years old.Copy the code
Fourth, the enumeration
Enumeration classes, also called Enumerations or enums, are special classes that represent a fixed number of constant values.
Description: 1. Define an enumeration type using the enum keyword. 2. Enumeration is a data type with a finite set of sequences. Each value in an enumeration has an index, which returns an index starting from 0. Each value in the enumeration can be listed using the values attribute
Limitations of enumerations: Enumerations cannot be subclassed, mixed, or implemented. Enumerations cannot be explicitly instantiated.
Enum Color {red, green, blue, Print (color.red.index); print(color.red.index); print(Color.green.index); print(Color.blue.index); // value print(Color.red); // Enumerate values List List = color. values; print(list); //[color.red, color.green, color.blue] Switch (color){case color. red: print(" red "); break; Case color. green: print(" green "); break; Case color. blue: print(" blue "); break; }}Copy the code
Fifth, with
Mixins are a way to reuse class code at different levels without inheritance. Simply put, it is to mix in other functions in the class to achieve similar functions of multiple inheritance.
Note: 1. Use the mixin keyword to replace class definitions with mixin class 2. A class that is a Mixin can only inherit from Object and cannot inherit from other classes. 3. Mixins are similar to multiple inheritance. 4. Classes that are mixins cannot declare constructors and cannot call Super 5. A class can mix multiple Mixin classes 6. Using the with method is similar to multiple inheritance 7. Mixins are neither inheritance nor interface, but a completely new feature
5.1 Mixing common classes
Use the keyword with to blend in ordinary classes
Class A{String name = "AA"; PrintA (){print(" This is ordinary class A"); } } class B{ String name = "BB"; PrintB (){print(" this is ordinary class B"); Void main(){class Student = new Student(); print(student.name); Student.printa (); // printA(); // printA(); // This is the ordinary class A student.printb (); // This is normal class B}Copy the code
5.2 Using Mixins to define mixins
// mixin A{String name = "AA"; PrintA (){print(" This is ordinary class A"); } } mixin B{ String name = "BB"; PrintB (){print(" this is ordinary class B"); }} class Student with A,B{} class Student with A,B{} class Student with A,B{} class Student with A,B{} print(student.name); // BB student.printA(); // This is the ordinary class A student.printb (); // This is normal class B}Copy the code
Notice that mixin classes can’t have constructors. Whether it’s a mixin class or a mixin class, it’s good to have constructors; However, if the class defined is not used as a mixin class, no error is reported
Mixin A{String name = "AA"; mixin A{String name = "AA"; // A(this.name); PrintA (){print(" this is ordinary class A"); }} class B{String name = "BB"; // B(this.name); PrintB (){print(" this is normal class B"); Class Student with A,B{}Copy the code
5.3 Mixin classes cannot inherit from other classes
Class Person{int age; Person(this.age); PrintInfo (){print(" inherited class Person, age ${this.age}"); Class B extends Person{printB(){print(" printB ") {print(" printB "); }} class Student with B{}Copy the code
5.4 Mixing does not affect inheritance
// mixin A{String name = "AA"; PrintA (){print(" This is ordinary class A"); } } class B{ String name = "BB"; PrintB (){print(" this is ordinary class B"); Class Person{int age; Person(this.age); PrintInfo (){print(" inherited class Person, age ${this.age}"); Class Student extends Person with A,B{Student(int age):super(age); } void main(){// instantiate class Student Student = new Student(18); // Inherit class student.printinfo (); // Print (student. Name); // AA student.printA(); // This is the ordinary class A student.printb (); // This is normal class B}Copy the code