@@ -10,62 +10,61 @@ nextpage:
10
10
title : Methods
11
11
---
12
12
13
- Constructors create instances of classes.
14
-
15
- * To declare a constructor, create a function named the same as its class.
16
- You can add an optional additional identifier as described in
17
- [ Named constructors] ( #named-constructors ) .
18
-
19
- * To instantiate a class, use the
20
- [ generative constructor] ( #generative-constructors ) .
21
-
22
- * To instantiate any instance variables,
23
- [ initialize formal parameters] ( #parameter-initialization ) .
13
+ Constructors are special functions that create instances of classes.
14
+
15
+ Dart implements many types of constructors.
16
+ Except for default constructors,
17
+ these functions use the same name as their class.
18
+
19
+ * [ Generative constructors] [ generative ] : Creates new instances and
20
+ initializes instance variables.
21
+ * [ Default constructors] [ default ] : Used to create a new instance when a
22
+ constructor hasn't been specified. It doesn't take arguments and
23
+ isn't named.
24
+ * [ Named constructors] [ named ] : Clarifies the purpose of
25
+ a constructor or allows the creation of multiple constructors for
26
+ the same class.
27
+ * [ Constant constructors] [ constant ] : Creates instances as compile-type
28
+ constants.
29
+ * [ Factory constructors] [ factory ] : Either creates a new instance of a
30
+ subtype or returns an existing instance from cache.
31
+ * [ Redirecting constructor] [ redirecting ] : Forwards calls to another
32
+ constructor in the same class.
33
+
34
+ [ default ] : #default-constructors
35
+ [ generative ] : #generative-constructors
36
+ [ named ] : #named-constructors
37
+ [ constant ] : #constant-constructors
38
+ [ factory ] : #factory-constructors
39
+ [ redirecting ] : #redirecting-constructors
24
40
25
41
<? code-excerpt path-base="misc/lib/language_tour/classes"?>
26
42
27
43
<? code-excerpt replace="/ *\/\/\s+ignore_for_file:[^\n]+\n//g; /(^|\n) *\/\/\s+ignore:[^\n]+\n/$1/g; /(\n[^\n]+) *\/\/\s+ignore:[^\n]+\n/$1\n/g; / *\/\/\s+ignore:[^\n]+//g; /([A-Z]\w*)\d\b/$1/g"?>
28
44
29
45
## Types of constructors
30
46
31
- ### Constructor inheritance
32
-
33
- _ Subclasses_ , or child classes, don't inherit * constructors*
34
- from their _ superclass_ , or immediate parent class.
35
- If a class doesn't declare a constructor, it can only use the
36
- [ default constructor] ( #default-constructors ) .
37
-
38
- A class can inherit the _ parameters_ of a superclass.
39
- These are called [ super parameters] ( #super-parameters )
40
- A subclass without constructor declarations can only use
41
- a [ default constructor] ( #default-constructors ) .
42
-
43
- Constructors work in a somewhat similar way to
44
- how you call a chain of static methods.
45
- Each subclass can call its superclass's constructor to initialize an instance,
46
- like a subclass can call a superclass's static method.
47
- This process doesn't involve any "inheritance".
48
-
49
- ### Default constructors
50
-
51
- If you don't declare a constructor, Dart uses the default constructor.
52
- The default constructor has no arguments and no name.
53
-
54
47
### Generative constructors
55
48
56
- To instantiate a class, use the generative constructor.
49
+ To instantiate a class, use a generative constructor.
57
50
58
51
<? code-excerpt "point_alt.dart (idiomatic-constructor)" plaster="none"?>
59
52
``` dart
60
53
class Point {
61
- double x = 0;
62
- double y = 0;
54
+ // Initializer list of variables and values
55
+ double x = 2.0;
56
+ double y = 2.0;
63
57
64
58
// Generative constructor with initializing formal parameters:
65
59
Point(this.x, this.y);
66
60
}
67
61
```
68
62
63
+ ### Default constructors
64
+
65
+ If you don't declare a constructor, Dart uses the default constructor.
66
+ The default constructor is a generative constructor without arguments or name.
67
+
69
68
### Named constructors
70
69
71
70
Use a named constructor to implement multiple constructors for a class
@@ -201,14 +200,14 @@ To further illustrate, consider the following example.
201
200
``` dart
202
201
// If you invoke the super constructor (`super(0)`) with any
203
202
// positional arguments, using a super parameter (`super.x`)
204
- // returns an error.
203
+ // results in an error.
205
204
Vector3d.xAxisError(super.x): z = 0, super(0); // BAD
206
205
```
207
206
208
207
This named constructor tries to set the ` x ` value twice:
209
208
once in the super constructor and once as a
210
209
positional super parameter.
211
- As both address the ` x ` positional parameter, this returns an error.
210
+ As both address the ` x ` positional parameter, this results in an error.
212
211
213
212
When the super constructor has named arguments, you can split them
214
213
between named super parameters (` super.y ` in the next example)
@@ -251,6 +250,7 @@ class ImmutablePoint {
251
250
```
252
251
253
252
Constant constructors don't always create constants.
253
+ They might be invoked in a non-` const ` context.
254
254
To learn more, consult the section on [ using constructors] [ ] .
255
255
256
256
### Redirecting constructors
@@ -274,7 +274,7 @@ class Point {
274
274
275
275
### Factory constructors
276
276
277
- When encountering one of two cases of implementing a constructor,
277
+ When encountering one of following two cases of implementing a constructor,
278
278
use the ` factory ` keyword:
279
279
280
280
* The constructor doesn't always create a new instance of its class.
@@ -359,22 +359,38 @@ Redirecting factories have several advantages:
359
359
* A redirecting factory constructor avoids the need for forwarders
360
360
to repeat the formal parameters and their default values.
361
361
362
- ## Parameter initialization
362
+ ## Constructor inheritance
363
+
364
+ _ Subclasses_ , or child classes, don't inherit * constructors*
365
+ from their _ superclass_ , or immediate parent class.
366
+ If a class doesn't declare a constructor, it can only use the
367
+ [ default constructor] ( #default-constructors ) .
368
+
369
+ A class can inherit the _ parameters_ of a superclass.
370
+ These are called [ super parameters] ( #super-parameters )
371
+
372
+ Constructors work in a somewhat similar way to
373
+ how you call a chain of static methods.
374
+ Each subclass can call its superclass's constructor to initialize an instance,
375
+ like a subclass can call a superclass's static method.
376
+ This process doesn't "inherit" constructor bodies or signatures.
377
+
378
+ ## Instance Variable Initialization
363
379
364
- Dart can initialize parameters in a constructor in three ways.
380
+ Dart can initialize variables in three ways.
365
381
366
- ### Initialize parameters when declaring variables
382
+ ### Initialize instance variables in the declaration
367
383
368
- Initialize the constructor parameters when you declare variables.
384
+ Initialize the instance variables when you declare the variables.
369
385
370
386
<? code-excerpt "point_alt.dart (initialize-declaration)" plaster="none"?>
371
387
``` dart
372
388
class PointA {
373
389
double x = 1.0;
374
390
double y = 2.0;
375
391
376
- // The parameterless constructor is not even be needed to set to (1.0,2.0)
377
- PointA();
392
+ // The implicit default constructor sets these variables to (1.0,2.0)
393
+ // PointA();
378
394
379
395
@override
380
396
String toString() {
@@ -419,7 +435,6 @@ class PointB {
419
435
420
436
// Initializing formal parameters can also be optional.
421
437
PointB.optional([this.x = 0.0, this.y = 0.0]);
422
- PointB.named({required this.x, required this.y});
423
438
}
424
439
```
425
440
@@ -451,16 +466,16 @@ class PointC {
451
466
452
467
// Generative constructor with initializing formal parameters
453
468
// with default values
454
- PointC({this.x = 1.0, this.y = 1.0});
469
+ PointC.named ({this.x = 1.0, this.y = 1.0});
455
470
456
471
@override
457
472
String toString() {
458
- return 'PointC($x,$y)';
473
+ return 'PointC.named ($x,$y)';
459
474
}
460
475
}
461
476
462
477
// Constructor using named variables.
463
- final pointC = PointC(x: 2.0, y: 2.0);
478
+ final pointC = PointC.named (x: 2.0, y: 2.0);
464
479
```
465
480
466
481
All variables introduced from initializing formal parameters are both
0 commit comments