-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolymorphism.py
More file actions
57 lines (42 loc) · 1.36 KB
/
Copy pathpolymorphism.py
File metadata and controls
57 lines (42 loc) · 1.36 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
class User:
# this class doesn't have an __init__
def sign_in(self):
return 'logged in'
# we can have same method names, but in different instances
# of the same class/sub-class, they can be overridden.
# For example, attack() method is defined here, which is
# again defined in Wizard and Archer classes too
def attack(self):
return f'do nothing'
# Wizard is subclass of User
class Wizard(User):
def __init__(self, name, power):
self.name = name
self.power = power
def attack(self):
return f'attacking with power of {self.power}'
# Archer is also a derived/sub class of User
class Archer(User):
def __init__(self, name, num_arrows):
self.name = name
self.num_arrows = num_arrows
def attack(self):
return f'attacking with arrows: arrows-left are {self.num_arrows}'
w1 = Wizard('Merlin', 50)
a1 = Archer('Robinhood', 100)
# Demonstration of Polymorphism
def player_attack(obj):
return obj.attack();
print(player_attack(w1)) # attacking with power of 50
print(player_attack(a1)) # attacking with arrows: arrows-left are 100
# Demo 2 of Polymorphism:
for obj in [w1, a1]:
print(player_attack(obj))
'''
Output:
------
attacking with power of 50
attacking with arrows: arrows-left are 100
attacking with power of 50
attacking with arrows: arrows-left are 100
'''