Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
This article also participated in the “Digitalstar Project” to win a creative gift package and creative incentive money.
📝 [Flutter] learning to form a record, [programmer essential knowledge]
📔 Today’s tip — Inheritance of Flutter in Dart!
1. Write at the front
You introduced class methods and object operators in Dart in the previous article, so let’s move on to learning about inheritance in Dart.
The Apple Mac is configured with the Flutter development environment
Android Studio installs a third-party simulator for Flutter — netease MuMu
Failed to find Build Tools Revision 29.0.2
Android license status unknown. Run ‘Flutter doctor — Android – Licenses’
How to create a Flutter project and run your first Flutter project
Dart uses var, final, and const
Dart Indicates the num of the data type
Dart String of data type
Dart data type list&Map
Dart method and arrow function
Dart’s method passes optional parameters and methods as parameters
Dart, Dart, Dart, Dart
Dart classes and objects in Flutter
Dart constructor of Flutter
Dart factory constructor & singleton & Initializer list
Dart class methods and object operators for Flutter
2. The inheritance
Inheritance is a common term in object-oriented development languages, and is often used for code reuse in iOS. How is inheritance defined and used in DART?
- In the Dart
- Inherit a class using Extens
- Subclasses inherit properties and methods in addition to the constructor
- Dart is single inheritance
So let’s look at the code in detail. Define a Person class. Student inherits the Person class.
class Student extends Person {
study(){
print("Study hard and make progress every day."); }}class Person {
String? name;
int? age;
int? _height;
// Calculate attributes
bool get isRight => _height! > 180;
run(){
print("run....."); }}Copy the code
2.1 Subclasses inherit the attributes and methods of their parent class
- Subclasses inherit the attributes and methods of their parent class
void main(a) {
Student stu = Student();
stu.name = "reno";
stu.run();
stu._height = 181;
stu.study();
print(stu.isRight);
}
Copy the code
- Print the result
2.2 Subclasses override superclass methods
- A subclass overrides a parent class method
@override
run(a){
print("Student run..");
}
Copy the code
Overriding the parent class method is the same as Swift using Override.
2.3 polymorphic
- polymorphism
/ / polymorphism
Person stu = Student();
if(stu is Student) {
stu.name = "reno";
stu.run();
stu._height = 181;
stu.study();
print(stu.isRight);
}
Copy the code
- The test results
All classes inherit from Object, so the Person class looks like this:
class Person extends Object{
}
Copy the code
Object has a toString() method similar to description in OC. Now rewrite toString() :
ToString () by default returns the value super.toString(), which can be customized
2.4 About the construction method
- A constructor
The default constructor of the parent class is inherited by default, but the constructor of the parent class is not inherited by default, and the constructor of the parent class is implemented manually. If there are multiple, the child class can implement one of them manually, as follows:
The subclass does not implement any constructors other than the default.
3. Write in the back
Follow me, more content continues to output
- CSDN
- The Denver nuggets
- Jane’s book
🌹 if you like, give it a thumbs up 👍🌹
🌹 feel harvest, can come to a wave of collection + attention, so as not to find you next time I 😁🌹
🌹 welcome everyone to leave a message exchange, criticism and correction, forwarding please indicate the source, thank you for your support! 🌹