6
6
///
7
7
// #docregion idiomatic-constructor
8
8
class Point {
9
- double x = 0 ;
10
- double y = 0 ;
9
+ // Initializer list of variables and values
10
+ double x = 2.0 ;
11
+ double y = 2.0 ;
11
12
12
13
// Generative constructor with initializing formal parameters:
13
14
Point (this .x, this .y);
@@ -34,3 +35,66 @@ class Point {
34
35
// #docregion idiomatic-constructor
35
36
}
36
37
// #enddocregion idiomatic-constructor
38
+
39
+ // #docregion initialize-declaration
40
+ class PointA {
41
+ double x = 1.0 ;
42
+ double y = 2.0 ;
43
+
44
+ // The implicit default constructor sets these variables to (1.0,2.0)
45
+ // PointA();
46
+
47
+ @override
48
+ String toString () {
49
+ return 'PointA($x ,$y )' ;
50
+ }
51
+ }
52
+ // #enddocregion initialize-declaration
53
+
54
+ // #docregion initialize-formal
55
+ class PointB {
56
+ final double x;
57
+ final double y;
58
+
59
+ // Sets the x and y instance variables
60
+ // before the constructor body runs.
61
+ PointB (this .x, this .y);
62
+
63
+ // Initializing formal parameters can also be optional.
64
+ PointB .optional ([this .x = 0.0 , this .y = 0.0 ]);
65
+ }
66
+ // #enddocregion initialize-formal
67
+
68
+ // #docregion initialize-named
69
+ class PointC {
70
+ double x; // must be set in constructor
71
+ double y; // must be set in constructor
72
+
73
+ // Generative constructor with initializing formal parameters
74
+ // with default values
75
+ PointC .named ({this .x = 1.0 , this .y = 1.0 });
76
+
77
+ @override
78
+ String toString () {
79
+ return 'PointC.named($x ,$y )' ;
80
+ }
81
+ }
82
+
83
+ // Constructor using named variables.
84
+ final pointC = PointC .named (x: 2.0 , y: 2.0 );
85
+ // #enddocregion initialize-named
86
+
87
+ // #docregion initialize-null
88
+ class PointD {
89
+ double ? x; // null if not set in constructor
90
+ double ? y; // null if not set in constructor
91
+
92
+ // Generative constructor with initializing formal parameters
93
+ PointD (this .x, this .y);
94
+
95
+ @override
96
+ String toString () {
97
+ return 'PointD($x ,$y )' ;
98
+ }
99
+ }
100
+ // #enddocregion initialize-null
0 commit comments