forked from realpython/materials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvehicles_polymorphism.py
More file actions
38 lines (28 loc) · 1.02 KB
/
Copy pathvehicles_polymorphism.py
File metadata and controls
38 lines (28 loc) · 1.02 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
class Vehicle:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
self._started = False
def start(self):
print("Starting engine...")
self._started = True
def stop(self):
print("Stopping engine...")
self._started = False
class Car(Vehicle):
def __init__(self, make, model, year, num_seats):
super().__init__(make, model, year)
self.num_seats = num_seats
def drive(self):
print(f'Driving my "{self.make} - {self.model}" on the road')
def __str__(self):
return f'"{self.make} - {self.model}" has {self.num_seats} seats'
class Motorcycle(Vehicle):
def __init__(self, make, model, year, num_wheels):
super().__init__(make, model, year)
self.num_wheels = num_wheels
def drive(self):
print(f'Riding my "{self.make} - {self.model}" on the road')
def __str__(self):
return f'"{self.make} - {self.model}" has {self.num_wheels} wheels'