Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Importer le module dart:math pour avoir accès à la fonction "sqrt"
- import 'dart:math' as math;
- // Définition de la classe Point.
- class Point {
- // Le mot-clé "final" fait que les variables ne peuvent être assignées qu'une seule fois.
- final num x, y;
- // Un constructeur : le sucre syntaxique permet d'initialiser les variables.
- Point(this.x, this.y);
- // Un constructeur nommé, avec une liste d'initialisation.
- Point.origin() : x = 0, y = 0;
- // Définition d'une méthode.
- num distanceTo(Point other) {
- var dx = x - other.x;
- var dy = y - other.y;
- return math.sqrt(dx * dx + dy * dy);
- }
- }
- // main() est le point d'entrée des programmes Dart.
- main() {
- // Instanciation des objets Point.
- 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