-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabstract-class.dart
62 lines (48 loc) · 1.47 KB
/
abstract-class.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Abstract Classes example
// If the powergrid only accepts nuclear plants but both nuclear and solar provide energy, therein lies the issue.
// Only cares about plants that qualify through abstract class
void main() {
// Make new powergrid
Powergrid grid = new Powergrid();
// Make new nuclear plant
NuclearPlant nuclear = new NuclearPlant();
// Make new solar plant
SolarPlant solar = new SolarPlant();
// Add a nuclear plant
grid.addPlants(nuclear);
grid.addPlants(solar);
}
class Powergrid {
// List of connected nuclear plants
List<PowerPlant> connectedPlants = [];
// Add a nuclear plant and turn it on
addPlants(PowerPlant plant) {
bool confirmation = plant.turnOn('5 hours');
connectedPlants.add(plant);
}
}
// A new type that says, if you have a 'turnOn' method, you qualify as a Powerplant
abstract class PowerPlant {
// Qualfiers
int costOfEnergy;
bool turnOn(String duration);
}
// Implements tells compiler that it will satisfy the interface defined by the abstract class Powerplant that it meets the requirements of a powerplant.
class NuclearPlant implements PowerPlant{
// Turn on plant method
int costOfEnergy;
bool turnOn(String timeToStayOn) {
print('Nuclear plant turning on.');
return true;
}
meltdown() {
print('Blowing up.');
}
}
class SolarPlant implements PowerPlant{
int costOfEnergy;
bool turnOn(String timeToStayOn) {
print('Solar plant turning on.');
return false;
}
}