Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Import the dart: math module to access the "sqrt" function.
- import 'dart:math' as math;
- // Definition of the Point class.
- class Point {
- // The keyword "final" means that variables can only be assigned once.
- final num x, y;
- // A constructor: the syntactic sugar allows to initialize the variables.
- Point(this.x, this.y);
- // A named constructor, with an initialization list.
- Point.origin() : x = 0, y = 0;
- // Definition of a method.
- num distanceTo(Point other) {
- var dx = x - other.x;
- var dy = y - other.y;
- return math.sqrt(dx * dx + dy * dy);
- }
- }
- // main() is the entry point for Dart programs.
- main() {
- // Instantiation of Point objects.
- var p1 = new Point(10, 10);
- var p2 = new Point.origin();
- var distance = p1.distanceTo(p2);
- print(distance);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement