Let's jump right in!
Go to https://dartpad.dartlang.org/ for online compilation.
We'll build a simple Dart class. Similar to Java: Bicycle class
Above the
main()
function, add a Bicycle
class with three instance variables. Also remove the contents from main()
, as shown in the following code snippet:class Bicycle {
int cadence;
int speed;
int gear;
}
void main() {
}
main()
.Bicycle(this.cadence, this.speed, this.gear);
;
) at the end of a no-body constructor, Dart displays the following error: "A function body must be provided.this
in a constructor's parameter list is a handy shortcut for assigning values to instance variables.The code above is equivalent to the following:
Bicycle(int cadence, int speed, int gear) {
this.cadence = cadence;
this.speed = speed;
this.gear = gear;
}
void main() {
var bike = Bicycle(2, 0, 1);
print(bike);
}
Run it. The following output comes:
Instance of 'Bicycle'
While the output "Instance of ‘Bicycle'" is correct, it's not very informative. All Dart classes have a
toString()
method that you can override to provide more useful output.@override
String toString() => 'Bicycle: $speed mph';
@override
annotation tells the analyzer that you are intentionally overriding a member. The analyzer raises an error if you failed to properly perform the override.${expression}
. If the expression is an identifier, you can skip the braces: $variableName
.=>
) notation.You should see the following output:
Bicycle: 0 mph
The original Java example defines
speed
as a read-only variable—it declares it as private and provides only a getter. Next, you'll provide the same functionality in Dart.To mark a Dart identifier as private to its library, start its name with an underscore (
_
). You can convert speed
to read-only by changing its name and adding a getter.In the Bicycle constructor, remove the speed parameter:
Bicycle(this.cadence, this.gear);
In
main()
, remove the second (speed
) parameter from the call to the Bicycle
constructor:var bike = Bicycle(2, 1);
Change the remaining occurrences of
speed
to _speed
. (Two places)Initialize
_speed
to 0: int _speed = 0;
Add the following getter to the
Bicycle
class:int get speed => _speed;
Observations
null
.cadence
and gear
. The Dart sample doesn't need explicit getters and setters for those, so it just uses instance variables. bike.cadence
, and later refactor it to use getters and setters. The API stays the same. In other words, going from a field to a getter and setter is not a breaking change in Dart.Finish implementing speed as a read-only instance variable
Add the following methods to the Bicycle class:
void applyBrake(int decrement) {
_speed -= decrement;
}
void speedUp(int increment) {
_speed += increment;
}
The final code:
class Bicycle {
int cadence;
int _speed = 0;
int get speed => _speed;
int gear;
Bicycle(this.cadence, this.gear);
void applyBrake(int decrement) {
_speed -= decrement;
}
void speedUp(int increment) {
_speed += increment;
}
@override
String toString() => 'Bicycle: $_speed mph';
}
void main() {
var bike = Bicycle(2, 1);
print(bike);
}
The final Dart example looks similar to the original Java, but is more compact at 23 lines instead of 40:
Thanks for reading...