- Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
1. Dart inheritance
Classes in Dart are single-inherited, using extends to extend a class, and subclasses inherit properties and methods in addition to the constructor.
Create a Person class.
class Person { String? name; int? age; int? _height; bool get isFree => _height! < 110; run() { print("run run run"); }Copy the code
Create a Student class inherited from Person.
class Student extends Person { study() { print("study study study"); }}Copy the code
Subclasses can access non-private properties and methods of their parent class,
Student s1 = Student();
s1.age = 20;
s1.run();
Copy the code
A subclass can override a method of its parent class, so that the subclass will call the overridden method.
@override // TODO: implement isFree bool get isFree => age! The < 18; @override run() { // TODO: implement run print("student run"); }Copy the code
Subclasses automatically inherit the default constructor. If it is a constructor with a name or argument, call it actively. The parent class
class Person { String? name; int? age; int? _height; bool get isFree => _height! < 110; run() { print("run run run"); } Person(this.age){ print("Person Person Person"); } Person.init(); }Copy the code
Subclasses (you can implement just one here) :
class Student extends Person { Student.init() : super.init(); Student(int? age) : super(age); study() { print("study study study"); } @override run() { // TODO: implement run print("student run"); } @override // TODO: implement isFree bool get isFree => age! The < 18; }Copy the code
2. Abstract classes and interfaces
Abstract classes are classes that cannot be instantiated. Use the abstract modifier.
Abstract methods can be declared in abstract classes.
Abstract class AbstractClass {int sum(int a,int b);Copy the code
Any other class that inherits from an abstract class must implement all the abstract methods in the abstract class.
class Subclass extends AbstractClass { @override int sum(int a, int b) { return a + b; }}Copy the code
The problem is that you can only inherit one abstract class. This is where you need implements to inherit multiple abstract classes.
class Subclass implements AbstractClass,AbstractClass1 { @override int sum(int a, int b) { return a + b; }}Copy the code
If implements a class, you need to implement all the properties and methods. Of course, this isn’t necessary; generally, only abstract classes are implemented.
3. With Mixins
Mixins are classes in object-oriented programming languages that provide implementations of methods. Other classes can access the methods and variables of a Mixin class without having to subclass it. Mixins are used to reuse class code across multiple class hierarchies. Mixins are all about multiple inheritance:
class D extends A with B,C {
}
Copy the code
This allows you to call methods in different classes.
D d = D();
d.a();
d.b();
d.c();
Copy the code
Here’s a question. If a method in three classes has the same name, which class will be called? C, that is, the method in the last inherited class will be called.
The limitation of mixin is that the class cannot implement the constructor, otherwise an error will be reported.
The class ‘C’ can’t be used as a mixin because it extends a class other than ‘Object’. This is because if C inherits a class that has a constructor with arguments, an error will be reported.
If D does not have its own properties and methods, then mixing can be abbreviated to.
class D = A with B,C;
Copy the code
4. Operator overloading
In Dart, if you want to compare the size of two classes or do something else, you need to use operator overloading. If there is no operator overload, the following code will report an error.
void operatorDemo() {
OperatorClass o1 = OperatorClass(22);
OperatorClass o2 = OperatorClass(23);
print(o1 > o2);
}
Copy the code
After overloading the > sign, you can compare sizes normally.
class OperatorClass {
int age;
OperatorClass(this.age);
bool operator >(OperatorClass other) => this.age > other.age;
}
Copy the code