Created with <3 with dartpad.dev.
Last active
July 21, 2022 07:33
-
-
Save mannuelf/8d4353bfff4b1542f868c3dbb76e0f30 to your computer and use it in GitHub Desktop.
abstract-classes
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
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