Content deleted Content added
MiguelMunoz (talk | contribs) →History: date for Dart 2.6 |
|||
Line 225:
<syntaxhighlight lang="dart">
void main() {
print('Hello, World!');
}
</syntaxhighlight>
Line 233:
<syntaxhighlight lang="dart">
void main() {
for (
print(i);
}
}
</syntaxhighlight>
Line 243:
<syntaxhighlight lang="dart">
void main() {
print('fibonacci($i) = ${fibonacci(i)}');
}
Line 264:
class Point {
// Final variables cannot be changed once they are assigned.
// Declare two instance variables.
final num x, y;
// A constructor, with syntactic sugar for setting instance variables.
// The constructor has two mandatory parameters.
Point(this.x, this.y);
// A named constructor with an initializer list.
Point.origin():
// A method.
num distanceTo(Point other) {
return math.sqrt(dx * dx + dy * dy);
}
// Example of a "getter".
// Acts the same as a final variable, but is computed on each access.
num get magnitude => math.sqrt(x * x + y * y);
// Example of operator overloading
Point operator +(Point other) => Point(x + other.x, y + other.y);
// When instantiating a class such as Point in Dart 2+, new is
// an optional word
}
// All Dart programs start with main().
void main() {
// Instantiate point objects.
print(p1.magnitude);
print(distance);
}
</syntaxhighlight>
|