@@ -22,6 +22,8 @@ Constructors create instances of classes.
22
22
* To instantiate any instance variables,
23
23
[ initialize formal parameters] ( #parameter-initialization ) .
24
24
25
+ <? code-excerpt path-base="misc/lib/language_tour/classes"?>
26
+
25
27
<? 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"?>
26
28
27
29
## Types of constructors
@@ -36,7 +38,7 @@ no-argument constructor in the superclass.
36
38
37
39
To instantiate a class, use the generative constructor.
38
40
39
- <? code-excerpt "misc/lib/language_tour/classes/ point_alt.dart (idiomatic-constructor)" plaster="none"?>
41
+ <? code-excerpt "point_alt.dart (idiomatic-constructor)" plaster="none"?>
40
42
``` dart
41
43
class Point {
42
44
double x = 0;
@@ -52,7 +54,7 @@ class Point {
52
54
Use a named constructor to implement multiple constructors for a class
53
55
or to provide extra clarity:
54
56
55
- <? code-excerpt "misc/lib/language_tour/classes/ point.dart (named-constructor)" replace="/Point\.\S*/[!$&!]/g" plaster="none"?>
57
+ <? code-excerpt "point.dart (named-constructor)" replace="/Point\.\S*/[!$&!]/g" plaster="none"?>
56
58
``` dart
57
59
const double xOrigin = 0;
58
60
const double yOrigin = 0;
@@ -93,7 +95,7 @@ In the following example,
93
95
the constructor for the ` Employee ` class calls the named constructor
94
96
for its superclass, ` Person ` . Click ** Run** to execute the code.
95
97
96
- <? code-excerpt "misc/lib/language_tour/classes/ employee.dart (super)" plaster="none"?>
98
+ <? code-excerpt "employee.dart (super)" plaster="none"?>
97
99
``` dart:run-dartpad:height-450px:ga_id-non_default_superclass_constructor
98
100
class Person {
99
101
String? firstName;
@@ -125,7 +127,7 @@ As Dart evaluates the arguments to the superclass constructor *before*
125
127
invoking the constructor, an argument can be an expression like a
126
128
function call.
127
129
128
- <? code-excerpt "misc/lib/language_tour/classes/ employee.dart (method-then-constructor)"?>
130
+ <? code-excerpt "employee.dart (method-then-constructor)"?>
129
131
``` dart
130
132
class Employee extends Person {
131
133
Employee() : super.fromJson(fetchDefaultData());
@@ -173,7 +175,7 @@ you must manually pass in all super constructor parameters.
173
175
If the super-constructor invocation includes positional arguments,
174
176
super-initializer parameters can't be positional.
175
177
176
- <? code-excerpt "misc/lib/language_tour/classes/ super_initializer_parameters.dart (positional)" plaster="none"?>
178
+ <? code-excerpt "super_initializer_parameters.dart (positional)" plaster="none"?>
177
179
``` dart
178
180
class Vector2d {
179
181
final double x;
@@ -209,7 +211,7 @@ between named super parameters (`super.y` in the next example)
209
211
and named arguments to the super constructor invocation
210
212
(` super.named(x: 0) ` ).
211
213
212
- <? code-excerpt "misc/lib/language_tour/classes/ super_initializer_parameters.dart (named)" plaster="none"?>
214
+ <? code-excerpt "super_initializer_parameters.dart (named)" plaster="none"?>
213
215
``` dart
214
216
class Vector2d {
215
217
// ...
@@ -235,7 +237,7 @@ empty, with the constructor call
235
237
(using ` this ` instead of the class name)
236
238
appearing after a colon (:).
237
239
238
- <? code-excerpt "misc/lib/language_tour/classes/ point_redirecting.dart"?>
240
+ <? code-excerpt "point_redirecting.dart"?>
239
241
``` dart
240
242
class Point {
241
243
double x, y;
@@ -254,7 +256,7 @@ If your class produces objects that never change, you can make these
254
256
objects compile-time constants. To do this, define a ` const ` constructor
255
257
and make sure that all instance variables are ` final ` .
256
258
257
- <? code-excerpt "misc/lib/language_tour/classes/ immutable_point.dart"?>
259
+ <? code-excerpt "immutable_point.dart"?>
258
260
``` dart
259
261
class ImmutablePoint {
260
262
static const ImmutablePoint origin = ImmutablePoint(0, 0);
@@ -296,7 +298,7 @@ In the following example includes two factory constructors.
296
298
* The ` Logger.fromJson ` factory constructor initializes a final variable
297
299
from a JSON object.
298
300
299
- <? code-excerpt "misc/lib/language_tour/classes/ logger.dart (constructors)"?>
301
+ <? code-excerpt "logger.dart (constructors)"?>
300
302
``` dart
301
303
class Logger {
302
304
final String name;
@@ -328,7 +330,7 @@ Factory constructors can't access to `this`.
328
330
329
331
Invoke a factory constructor just like you would any other constructor:
330
332
331
- <? code-excerpt "misc/lib/language_tour/classes/ logger.dart (logger)"?>
333
+ <? code-excerpt "logger.dart (logger)"?>
332
334
``` dart
333
335
var logger = Logger('UI');
334
336
logger.log('Button clicked');
@@ -345,7 +347,7 @@ Dart can initialize parameters in a constructor in three ways.
345
347
346
348
Initialize the constructor parameters when you declare variables.
347
349
348
- <? code-excerpt "misc/lib/language_tour/classes/ point_alt.dart (initialize-declaration)" plaster="none"?>
350
+ <? code-excerpt "point_alt.dart (initialize-declaration)" plaster="none"?>
349
351
``` dart
350
352
class PointA {
351
353
[!double x = 1.0;!]
@@ -385,7 +387,7 @@ Initializing formal parameters also allow you to initialize
385
387
non-nullable or ` final ` instance variables.
386
388
Both of these types of variables require initialization or a default value.
387
389
388
- <? code-excerpt "misc/lib/language_tour/classes/ point_alt.dart (initialize-formal)" plaster="none"?>
390
+ <? code-excerpt "point_alt.dart (initialize-formal)" plaster="none"?>
389
391
``` dart
390
392
class PointB {
391
393
final double x;
@@ -408,7 +410,7 @@ class PointB {
408
410
409
411
This also works with named variables.
410
412
411
- <? code-excerpt "misc/lib/language_tour/classes/ point_alt.dart (initialize-named)" plaster="none"?>
413
+ <? code-excerpt "point_alt.dart (initialize-named)" plaster="none"?>
412
414
``` dart
413
415
class PointC {
414
416
double x; // must be set in constructor
@@ -438,7 +440,7 @@ You can then pass the computed values to a normal constructor.
438
440
439
441
The constructor parameters could be set as nullable and not be initialized.
440
442
441
- <? code-excerpt "misc/lib/language_tour/classes/ point_alt.dart (initialize-null)" plaster="none"?>
443
+ <? code-excerpt "point_alt.dart (initialize-null)" plaster="none"?>
442
444
``` dart
443
445
class PointD {
444
446
[!double? x;!] // null if not set in constructor
@@ -459,7 +461,7 @@ class PointD {
459
461
Before the constructor body runs, you can initialize instance variables.
460
462
Separate initializers with commas.
461
463
462
- <? code-excerpt "misc/lib/language_tour/classes/ point_alt.dart (initializer-list)"?>
464
+ <? code-excerpt "point_alt.dart (initializer-list)"?>
463
465
``` dart
464
466
// Initializer list sets instance variables before
465
467
// the constructor body runs.
@@ -477,7 +479,7 @@ The right-hand side of an initializer list can't access `this`.
477
479
To validate inputs during development,
478
480
use ` assert ` in the initializer list.
479
481
480
- <? code-excerpt "misc/lib/language_tour/classes/ point_alt.dart (initializer-list-with-assert)" replace="/assert\(.*?\)/[!$&!]/g"?>
482
+ <? code-excerpt "point_alt.dart (initializer-list-with-assert)" replace="/assert\(.*?\)/[!$&!]/g"?>
481
483
``` dart
482
484
Point.withAssert(this.x, this.y) : [!assert(x >= 0)!] {
483
485
print('In Point.withAssert(): ($x, $y)');
@@ -489,7 +491,7 @@ Initializer lists help set up `final` fields.
489
491
The following example initializes three ` final ` fields in an initializer list.
490
492
To execute the code, click ** Run** .
491
493
492
- <? code-excerpt "misc/lib/language_tour/classes/ point_with_distance_field.dart"?>
494
+ <? code-excerpt "point_with_distance_field.dart"?>
493
495
``` dart:run-dartpad:height-340px:ga_id-initializer_list
494
496
import 'dart:math';
495
497
0 commit comments