Created
May 11, 2020 13:04
-
-
Save swavkulinski/0e78566237aa0bacbdc9673f1f0d4203 to your computer and use it in GitHub Desktop.
This file contains 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
void main() { | |
var breakfast = Breakfast( | |
tea: Tea( | |
milk: Milk(), | |
), | |
); | |
print('$breakfast was ${breakfast.getCalories()} calories'); | |
} | |
abstract class Meal { | |
int getCalories(); | |
} | |
class Breakfast extends Meal { | |
Tea tea; | |
Breakfast({this.tea}); | |
@override int getCalories() { | |
if (tea != null) return tea.getCalories(); | |
return 0; | |
} | |
@override | |
String toString() => 'Breakfast'; | |
} | |
class Tea extends Meal { | |
Milk milk; | |
Tea({this.milk}); | |
@override int getCalories() { | |
if (milk != null) return milk.getCalories(); | |
return 0; | |
} | |
@override | |
String toString() => 'Tea'; | |
} | |
class Milk extends Meal { | |
// default constructor Milk() is added by compiler | |
@override int getCalories() => 20; | |
@override | |
String toString() => 'Milk'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment