-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsection_11.py
More file actions
61 lines (45 loc) · 1.49 KB
/
section_11.py
File metadata and controls
61 lines (45 loc) · 1.49 KB
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
# Polymorphism
class Vehicle:
def __init__(self, brand, model, year):
self.brand = brand
self.model = model
self.year = year
def start(self):
print(f'{self.__class__.__name__}: is starting' )
def stop(self):
print(f'{self.__class__.__name__}: is stopping' )
class Car(Vehicle):
def __init__(self, brand, model, year, wheels, doors):
super().__init__(brand, model, year)
self.wheels = wheels
self.doors = doors
def stop(self):
print(f'{self.__class__.__name__}: Method overide' )
class Motorcycle(Vehicle):
def __init__(self, brand, model, year, wheels):
super().__init__(brand, model, year)
self.wheels = wheels
def start(self):
print(f'{self.__class__.__name__}: Method overide' )
############
### MAIN ###
############
if __name__ == '__main__':
# # Uncomment to test
# car_01 = Car('Brand_C', 'Model_C', 2020, 3, 4)
# print(car_01.__dict__)
# car_01.start()
# # Uncomment to test
# bike_01 = Motorcycle('Brand_M', 'Model_M', 2010, 2)
# print(bike_01.__dict__)
# bike_01.start()
# Uncomment to test
# Type hinting resticts elements of the list to Vehicle types
vehicles: list[Vehicle] = [
Car('BrandCC', 'ModelCC', 2018, 4, 4),
Motorcycle('BrandMM', 'ModelMM', 2021, 2)
]
for v in vehicles:
print(v.__dict__)
v.start()
v.stop()