Last active
May 5, 2020 15:10
-
-
Save mihalycsaba/54b77db084b5fb476197b9d2eca2cc4a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person { | |
DateTime birthday; | |
String name; | |
final int days = 365; | |
Person(this.name, {this.birthday}); | |
set age(double years) { | |
var d = new DateTime.now(); | |
var dur = new Duration(days: (days * years).toInt()); | |
d = d.subtract(dur); | |
birthday = d; | |
} | |
double get age { | |
var d = new DateTime.now(); | |
return d.difference(birthday).inDays / days; | |
} | |
double myage() { | |
var d = new DateTime.now(); | |
return d.difference(birthday).inDays / days; | |
} | |
} | |
main() { | |
var p = new Person("Foo"); | |
print(p.name); | |
p.age = 18.0; | |
print(p.age); | |
print(p.myage()); // () required because it is not a real getter | |
var o = new Person("Bar", birthday: new DateTime.now()); | |
print(o.name); | |
print(o.birthday); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment