Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 0.91 KB | None | 0 0
  1. // Importer le module dart:math pour avoir accès à la fonction "sqrt"
  2. import 'dart:math' as math;
  3.  
  4. // Définition de la classe Point.
  5. class Point {
  6.  
  7.     // Le mot-clé "final" fait que les variables ne peuvent être assignées qu'une seule fois.
  8.     final num x, y;
  9.    
  10.     // Un constructeur : le sucre syntaxique permet d'initialiser les variables.
  11.     Point(this.x, this.y);
  12.    
  13.     // Un constructeur nommé, avec une liste d'initialisation.
  14.     Point.origin() : x = 0, y = 0;
  15.    
  16.     // Définition d'une méthode.
  17.     num distanceTo(Point other) {
  18.         var dx = x - other.x;
  19.         var dy = y - other.y;
  20.         return math.sqrt(dx * dx + dy * dy);
  21.     }
  22. }
  23.  
  24. // main() est le point d'entrée des programmes Dart.
  25. main() {
  26.     // Instanciation des objets Point.
  27.     var p1 = new Point(10, 10);
  28.     var p2 = new Point.origin();
  29.     var distance = p1.distanceTo(p2);
  30.     print(distance);
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement