Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Alternative constructor

class Person {
  DateTime birthday;
  String name;
  final int DAYS = 365;
  
  Person(this.name, {this.birthday});

  Person.byAge(this.name, double years) {
    var d = new DateTime.now();
    var dur = new Duration(days: (DAYS*years).toInt());
    d = d.subtract(dur);
    birthday = d;
  }  
}

main() {
  var p = new Person.byAge("Foo", 18.0);
  print(p.name);
  print(p.birthday); 
  
  var o = new Person("Bar", birthday: new DateTime.now());
  print(o.name);
  print(o.birthday);
}