-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeek-5-OOP-2.py
More file actions
80 lines (60 loc) · 2.31 KB
/
Week-5-OOP-2.py
File metadata and controls
80 lines (60 loc) · 2.31 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
class Vehicle:
"""Base class for all vehicles"""
def __init__(self, name, max_speed):
self.name = name
self.max_speed = max_speed
def move(self):
"""Base movement method"""
print(f"{self.name} is moving")
def describe(self):
"""Describe the vehicle"""
print(f"{self.name} (max speed: {self.max_speed} km/h)")
class Car(Vehicle):
def __init__(self, name, max_speed, num_wheels=4):
super().__init__(name, max_speed)
self.num_wheels = num_wheels
def move(self):
"""Car-specific movement"""
print(f"{self.name} is driving on {self.num_wheels} wheels at {self.max_speed} km/h")
def honk(self):
"""Car-specific method"""
print(f"{self.name} honks: BEEP BEEP!")
class Plane(Vehicle):
def __init__(self, name, max_speed, max_altitude):
super().__init__(name, max_speed)
self.max_altitude = max_altitude
def move(self):
"""Plane-specific movement"""
print(f"{self.name} is flying at altitude {self.max_altitude}m")
def land(self):
"""Plane-specific method"""
print(f"{self.name} is landing on the runway")
class Boat(Vehicle):
def __init__(self, name, max_speed, boat_type):
super().__init__(name, max_speed)
self.boat_type = boat_type
def move(self):
"""Boat-specific movement"""
print(f"{self.name} is sailing as a {self.boat_type} at {self.max_speed} km/h")
def anchor(self):
"""Boat-specific method"""
print(f"{self.name} drops anchor")
# Create vehicle instances
sedan = Car(name="Toyota", max_speed=180)
jet = Plane(name="Boeing 747", max_speed=900, max_altitude=12000)
yacht = Boat(name="ferry", max_speed=70, boat_type="yacht")
# Demonstrate polymorphism
def start_journey(vehicle):
"""Function that works with any Vehicle object"""
print(f"\n--- {vehicle.name} Journey ---")
vehicle.describe()
vehicle.move() # This calls the appropriate move() method based on the object type
# Call the same method on different vehicle types
vehicles = [sedan, jet, yacht]
for vehicle in vehicles:
start_journey(vehicle)
# Demonstrate unique methods
print("\n--- Vehicle-specific actions ---")
sedan.honk()
jet.land()
yacht.anchor()