Skip to content

Instantly share code, notes, and snippets.

@swavkulinski
Created May 11, 2020 13:04
Show Gist options
  • Save swavkulinski/0e78566237aa0bacbdc9673f1f0d4203 to your computer and use it in GitHub Desktop.
Save swavkulinski/0e78566237aa0bacbdc9673f1f0d4203 to your computer and use it in GitHub Desktop.
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