Key points:
- Implements must implement all properties and methods (ordinary and abstract) in the interface class.
This blog to write well: www.jianshu.com/p/18e8d285c…
- Java vs. Dart: When designing relationships between classes using the Java language, we are introduced to the concepts of constituent units and relational connections:
Components: common class, abstract class, interface. Relational connection: implements, extends extends. In Dart, two concepts are added and subtracted: component: common class, Abstract abstract class, and mixin. Relational connection: implements, extends, with. The biggest differences are two:
Interface is removed. Added the concept of blending.
1. Component unit
-
The generic Java class is no different from Dart
-
Dart’s abstract classes are defined in much the same way as Java’s abstract classes. You can define variables, ordinary methods, and abstract methods.
Java and Dart differ in using abstract classes in one way: Dart does not need the abstract modifier when defining abstract methods.
abstract class DartAbs { void absMethod(); // No implementation body method - abstract method, Dart does not need abstract modifier}Copy the code
- Dart does not use the interface keyword to define an interface class. Common and abstract classes can be used as interface classes. By the way, let’s review some key points about Abstract and interface in Java:
- Neither abstract classes nor interfaces can be instantiated.
- Abstract classes are inherited by subclasses and interfaces are implemented by classes.
- Interface can only do method declaration, abstract class can do method declaration, can also do method implementation.
- Variables defined in an interface can only be public static constants. Variables in an abstract class can be ordinary variables.
- Abstract methods in an abstract class must all be implemented by subclasses; The interface methods of an interface must all be implemented by subclasses, otherwise they are abstract classes.
- Abstract classes can have no abstract methods.
- If a class has abstract methods in it, that class must be abstract.
- Abstract methods are implemented, so they cannot be static or private.
- An interface can inherit from an interface and can inherit from more than one interface, but a class can only inherit from one interface.
2. Connection relationship
- Extends Inheritance Dart, like Java, is a single inheritance. Subclasses can inherit properties and methods visible from their parent class. For Java, visible means a non-private modifier, and for Dart, it means a non-underscore _ beginning. The subclass calls the methods of the parent class, using the super keyword. Subclasses do not inherit the constructor of their parent class.
Methods that inherit from a parent class need not be overridden
- Implements all the attributes and methods (ordinary or abstract) in B, and does not need to be overwritten. If you want to create A class A that supports the API of class B but doesn’t need to inherit the implementation of B, you can implement the interface of implements B through A.
class Implements {
String a;
void base() {
print('base');
}
void log() {
print('extends');
}
}
class Log implements Implements {
String a = "hello"; // Without this statement, an error is reported because all properties and methods in the interface class must be implementedbase() {
print('log#base');
}
log() {
print('log' + a);
}
}
void main() {
Log().log();
}
Copy the code
Implement a mixin by creating a class that inherits from Object and has no constructor. If mixins do not want to be used as regular classes, use the keyword Mixin instead of class. That is, a class mixed with with must not have a constructor. It can be class-defined, but it is better to use the mixin keyword to define such a class.
Mixin MusicalPerformer on Musician {// ···}Copy the code
The class mixin on mixed MusicalPerformer must inherit Musician as follows:
abstract class Super {
void method() {
print("Super");
}
}
class MySuper implements Super {
void method() {
print("MySuper");
}
}
mixin Mixin on Super {
void method() { super.method(); / / 15 linesprint("Sub"); } class extends MySuper with Mixin {} Outputs MySuper SubCopy the code
The call to super.method() at line 15 looks for a method in MyMixin, finds one, and since MyMixin restricts subclasses to inherit or implement super, So the super.method() method in line 15 calls the method method of MyMixin’s subclass extend, in this case MySuper’s method() method, so MySuper and then Sub.
If extends and with both exist and they define the same method name, the with modifier overrides the extends method of the same name. The next in the with list overwrites the previous one.
Plus implements
class Extends {
void log() {
print('extends');
}
}
mixin Mixins {
void log() {
print('mixin');
}
}
mixin Mixins2 {
void log() {
print('mixin2');
}
}
class Implements {
void log() {
print('implements');
}
}
class Log extends Extends with Mixins, Mixins2 implements Implements {}
void main() {
Log().log();
}
Copy the code
Mixin2 is displayed
Dart doesn’t let us implement the implements.log() method, even though we added implements!
This is because in this case, it recognizes that we have the ability to get the log() method from with and extends, and therefore calls mixins2.log ().
If we implement the Implements#log method,
class Log extends Extends with Mixins, Mixins2 implements Implements {
void log() {
print("implements log"); }}Copy the code
The result is: implements log