Skip to content

Instantly share code, notes, and snippets.

@mannuelf
Last active July 21, 2022 07:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mannuelf/8d4353bfff4b1542f868c3dbb76e0f30 to your computer and use it in GitHub Desktop.
Save mannuelf/8d4353bfff4b1542f868c3dbb76e0f30 to your computer and use it in GitHub Desktop.
abstract-classes
void main() {
PowerGrid grid = PowerGrid();
NuclearPlant nuclear = NuclearPlant();
SolarPlant solar = SolarPlant();
grid.addPlant(nuclear);
grid.addPlant(solar);
}
class PowerGrid {
List<PowerPlant> connectedPlants = [];
addPlant(PowerPlant plant) {
bool confirmation = plant.turnOn('6 hours');
connectedPlants.add(plant);
}
}
abstract class PowerPlant {
int costOfEnergy = 0;
bool turnOn(String duration);
}
class NuclearPlant implements PowerPlant {
int costOfEnergy = 100;
bool turnOn(String operationTime) {
print('☢️ Nuclear plant on for $operationTime at a cost of $costOfEnergy');
return true;
}
}
class SolarPlant implements PowerPlant {
int costOfEnergy = 50;
bool turnOn(String operationTime) {
print('🌞 Solar plant on for $operationTime at a cost of $costOfEnergy');
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment