Dart by Example: Getters and Setters

  1. import 'dart:math';
  2. class Position {
  3. int _x;
  4. int _y;
  5. Position(this._x, this._y);
  6. double get rad => atan2(_y, _x);
  7. void set x(int val) {
  8. _x = val;
  9. }
  10. }
  11. main() {
  12. var p = new Position(2, 3);
  13. p.x = 10;
  14. print('x: ${p._x} y: ${p._y}');
  15. print('rad: ${p.rad}');
  16. }
  17.  
  1. $ getters_setters.dart
  2. x: 10 y: 3
  3. rad: 0.2914567944778671

by @jryanio | source | license