Dart by Example: Classes

  1. import 'dart:math';
  2. class Position {
  3. // properties
  4. int x;
  5. int y;
  6. // methods
  7. double distanceTo(Position other) {
  8. var dx = other.x - x;
  9. var dy = other.y - y;
  10. return sqrt(dx * dx + dy * dy);
  11. }
  12. }
  13. main() {
  14. var origin = new Position()
  15. ..x = 0
  16. ..y = 0;
  17. var p = new Position()
  18. ..x = -5
  19. ..y = 6;
  20. print(origin.distanceTo(p));
  21. }
  22.  
  1. $ dart classes.dart
  2. 7.810249675906654

by @jryanio | source | license