-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClass_oop.py
More file actions
111 lines (82 loc) · 2.76 KB
/
Class_oop.py
File metadata and controls
111 lines (82 loc) · 2.76 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
class Literature:
"""Base class for literary works"""
def __init__(self, title, author, year):
self.title = title
self.author = author
self.year = year
self.__secret_meaning = "Hidden symbolism"
def analyze(self):
return f"Analyzing {self.title} by {self.author}"
def reveal_secret(self):
return self.__secret_meaning
class ShakespearePlay(Literature):
"""Shakespeare literature specialization"""
def __init__(self, title, year, genre):
super().__init__(title, "William Shakespeare", year)
self.genre = genre
self.famous_quote = "To be or not to be"
def perform(self):
return f"Performing {self.title} in iambic pentameter!"
def analyze(self):
return f"Shakespearean analysis of {self.title}"
class Animal:
"""Base class for animals"""
def __init__(self, name, habitat):
self.name = name
self.habitat = habitat
def move(self):
return "Generic animal movement"
def communicate(self):
return "Generic animal sound"
class Dolphin(Animal):
"""Dolphin specialization"""
def __init__(self, name, pod_name):
super().__init__(name, "Ocean")
self.pod_name = pod_name
self.intelligence = "High"
def communicate(self):
return "Eee-eee-eee clicks and whistles!"
def jump(self):
return f"{self.name} leaps gracefully out of the water!"
class Superhero:
"""Base class for superheroes"""
def __init__(self, secret_identity, origin):
self.__secret_identity = secret_identity
self.origin = origin
def reveal_identity(self):
return self.__secret_identity
def use_power(self):
return "Generic superhero power"
class Spiderman(Superhero):
"""Spiderman specialization"""
def __init__(self):
super().__init__("Peter Parker", "Radioactive spider bite")
self.catchphrase = "With great power comes great responsibility"
def use_power(self):
return "Thwip! Shoots web from wrists"
def swing(self):
return "Swings between skyscrapers with web"
class Vehicle:
def move(self):
pass
class Car(Vehicle):
def move(self):
return "Driving 🚗"
class Plane(Vehicle):
def move(self):
return "Flying ✈️"
class Boat(Vehicle):
def move(self):
return "Sailing ⛵"
class Bicycle(Vehicle):
def move(self):
return "Pedaling 🚲"
# Polymorphism in action!
def travel(vehicle):
print(vehicle.move())
# Create instances
vehicles = [Car(), Plane(), Boat(), Bicycle()]
# Test polymorphism
print("Let's go on a trip!")
for vehicle in vehicles:
travel(vehicle)