Skip to content

Instantly share code, notes, and snippets.

@mustofa-id
Last active January 24, 2019 07:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mustofa-id/1d6fee69d2e94e350f4f396d5d519427 to your computer and use it in GitHub Desktop.
Save mustofa-id/1d6fee69d2e94e350f4f396d5d519427 to your computer and use it in GitHub Desktop.
Learn dart language as Java Developers
class Bicycle {
int cadence;
int _speed = 0; // Private
int gear;
Bicycle(this.cadence, this.gear);
int get speed => _speed; // Getter
void applyBreake(int dec) {
_speed -= dec;
}
void speedUp(int inc) {
_speed += inc;
}
@override
toString() => 'Bicycle: $_speed mph';
}
void main() {
var bike = Bicycle(2, 1);
bike.speedUp(5);
bike.applyBreake(1);
print(bike);
}
import 'dart:math';
class Rectangle {
Point origin;
int width;
int height;
// Using opotional params (Like overloading in Java)
Rectangle({
this.origin = const Point(0, 0),
this.width = 0,
this.height = 0
});
@override
String toString() =>
'Origin: (${origin.x}, ${origin.y}), width: $width, height: $height';
}
void main() {
print(Rectangle(origin: const Point(10, 20), width: 100, height: 200));
print(Rectangle(origin: const Point(10, 10)));
print(Rectangle(width: 200));
print(Rectangle());
}
import 'dart:math';
abstract class Shape {
num get area;
}
class Circle implements Shape {
final num radius;
Circle(this.radius);
num get area => pi * pow(radius, 2);
}
class Square implements Shape {
final num side;
Square(this.side);
num get area => pow(side, 2);
}
void main() {
final circle = Circle(2);
print(circle.area);
final square = Square(2);
print(square.area);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment