Skip to content

Commit 89ba313

Browse files
author
Tony Sansone
committed
Refactor Constructors page
1 parent be334a4 commit 89ba313

File tree

4 files changed

+328
-168
lines changed

4 files changed

+328
-168
lines changed

examples/misc/lib/language_tour/classes/point_alt.dart

+69
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,72 @@ class Point {
3434
// #docregion idiomatic-constructor
3535
}
3636
// #enddocregion idiomatic-constructor
37+
38+
// #docregion initialize-declaration
39+
class PointA {
40+
double x = 1.0;
41+
double y = 2.0;
42+
43+
// The parameterless constructor is not even be needed to set to (1.0,2.0)
44+
PointA();
45+
46+
@override
47+
String toString() {
48+
return 'PointA($x,$y)';
49+
}
50+
}
51+
// #enddocregion initialize-declaration
52+
53+
// #docregion initialize-formal
54+
class PointB {
55+
final double x;
56+
final double y;
57+
58+
// Sets the x and y instance variables
59+
// before the constructor body runs.
60+
PointB(this.x, this.y);
61+
62+
// Initializing formal parameters can also be optional.
63+
PointB.optional([this.x = 0.0, this.y = 0.0]);
64+
PointB.named({required this.x, required this.y});
65+
66+
// Private fields cannot be used as named initializing formals.
67+
PointB.namedPrivate({required double x, required double y})
68+
: _x = x,
69+
_y = y;
70+
}
71+
// #enddocregion initialize-formal
72+
73+
// #docregion initialize-named
74+
class PointC {
75+
double x; // must be set in constructor
76+
double y; // must be set in constructor
77+
78+
// Generative constructor with initializing formal parameters
79+
// with default values
80+
PointC({this.x = 1.0, this.y = 1.0});
81+
82+
@override
83+
String toString() {
84+
return 'PointC($x,$y)';
85+
}
86+
}
87+
88+
// Constructor using named variables.
89+
final pointC = PointC(x: 2.0, y: 2.0);
90+
// #enddocregion initialize-named
91+
92+
// #docregion initialize-null
93+
class PointD {
94+
double? x; // null if not set in constructor
95+
double? y; // null if not set in constructor
96+
97+
// Generative constructor with initializing formal parameters
98+
PointD(this.x, this.y);
99+
100+
@override
101+
String toString() {
102+
return 'PointD($x,$y)';
103+
}
104+
}
105+
// #enddocregion initialize-null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// #docregion named
2+
class Vector2d {
3+
// #enddocregion named
4+
final double x;
5+
final double y;
6+
7+
Vector2d(this.x, this.y);
8+
// #docregion named
9+
// ...
10+
Vector2d.named({required this.x, required this.y});
11+
}
12+
13+
class Vector3d extends Vector2d {
14+
final double z;
15+
16+
// Forward the y parameter to the named super constructor like:
17+
// Vector3d.yzPlane({required double y, required this.z})
18+
// : super.named(x: 0, y: y);
19+
Vector3d.yzPlane({required super.y, required this.z}) : super.named(x: 0);
20+
}
21+
// #enddocregion named
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// #docregion positional
2+
class Vector2d {
3+
final double x;
4+
final double y;
5+
6+
Vector2d(this.x, this.y);
7+
}
8+
9+
class Vector3d extends Vector2d {
10+
final double z;
11+
12+
// Forward the x and y parameters to the default super constructor like:
13+
// Vector3d(final double x, final double y, this.z) : super(x, y);
14+
Vector3d(super.x, super.y, this.z);
15+
}
16+
// #enddocregion positional

0 commit comments

Comments
 (0)