“This is the 9th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”
Introduction to the
As an object-oriented language, Class is essential to DART. All classes in DART, except Null, inherit from Object Class. To use a class in DART, you need to construct an instance of the class. In DART, a class constructor can be constructed in two ways.
Traditional constructors
Dart, like JAVA, can use a function with the same name as the class as its constructor. This is how many programming languages create the first constructor. Let’s use the Student class as an example to see how dart’s constructor works:
class Student { int age = 0; int id = 0; Point(int age, int id) { this.age = age; this.id = id; }}Copy the code
The above this represents an instance of the current class. For Dart, this is negligible, but in the above example, because the name of the class variable is the same as the name of the argument passed in to the constructor, this is needed to differentiate.
The above code is simple, but it’s too much to write. Here’s a shorthand for dart:
class Student {
int age = 0;
int id = 0;
Student(this.age, this.id);
}
Copy the code
Of course, you can also omit the constructor, in which case Dart will create a default constructor with no arguments for you.
Named constructor
Dart differs from other languages in that you can also use named constructors. The named constructor is of the form classname.identifier, as follows:
class Student { int age = 0; int id = 0; Student(this.age, this.id); Student.fromJson(Map data) { print('in Student'); }}Copy the code
The Student. FromJson above is a named constructor. You can use this constructor to generate a Student object from the Map, somewhat like the Factory method in Java.
The order in which constructors are executed
Dart classes are inheritable, so what is the order of execution of constructors for subclasses of DART?
If you do not specify a constructor for the Dart class, Dart automatically generates a no-argument constructor for the class, and if the class is a subclass, dart automatically calls the no-argument constructor for the parent class.
There are three steps to initialize a subclass constructor:
- Call the initializer list
- Call the parent class’s constructor
- Call your own constructor
In Step 2, if the parent class does not have a default no-argument constructor, you need to manually specify the constructor for the concrete parent class. How do I call it? We can use it directly after the constructor of a subclass: the operator follows the constructor of the parent class, as follows:
class Student { String? firstName; Student.fromJson(Map data) { print('in Student'); } } class Jone extends Student { Jone.fromJson(Map data) : super.fromJson(data) { print('in Jone'); }}Copy the code
Now that we understand the parent constructor, what is an initializer list?
The initializer is the code that is executed before the constructor is executed, using the same: operator as when the parent constructor is called, as shown below:
Point.fromJson(Map<String, double> json) : x = json['x']! , y = json['y']! { print('In Point.fromJson(): ($x, $y)'); }Copy the code
Redirect constructor
If a constructor needs to call another constructor without making any changes, it can use the redirection constructor. The redirection constructor also uses the: operator, followed by another constructor:
class Point { double x, y; // Point(this.x, this.y); AlongXAxis (double x) : this(x, 0); }Copy the code
Constant constructor
If a property in an object does not change after creation, we can use the Constant constructor, which is preceded by the const modifier. All initialized properties are final:
class ImmutablePoint {
static const ImmutablePoint origin = ImmutablePoint(0, 0);
final double x, y;
const ImmutablePoint(this.x, this.y);
}
Copy the code
Factory constructor
By default, the constructor in the DART class returns a new instance of the class, but in practice we may have to make some choices about what to return, such as returning an existing object from the cache or an implementation subclass of the class.
Dart has a dedicated Factory keyword for such functionality, and constructors that use Factory are called Factory constructors.
class Student {
final String name;
static final Map<String, Student> _studentMap =
<String, Student>{};
factory Student(String name) {
return _studentMap.putIfAbsent(
name, () => Student._newStudent(name));
}
factory Student.fromJson(Map<String, Object> json) {
return Student(json['name'].toString());
}
Student._newStudent(this.name);
}
Copy the code
Note that dart can have only one unnamed constructor. The name must not be repeated for named functions, otherwise The default constructor is already defined exception will be reported.
In the above code, Factory Student is an unnamed constructor and Factory Student.fromjson is a named constructor.
So if you add an unnamed constructor to Student as follows:
Student(this.name);
Copy the code
An error is reported.
So what’s the difference between a factory constructor and a normal constructor?
The main difference is that the normal constructor has no return value, whereas the factory constructor requires a return value.
conclusion
These are the various constructors in DART and the issues to be aware of when using them.
This article is available at www.flydean.com/06-dart-cla…
The most popular interpretation, the most profound dry goods, the most concise tutorial, many tips you didn’t know waiting for you to discover!
Welcome to pay attention to my public number: “procedures those things”, understand technology, more understand you!