-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinheritance.py
50 lines (34 loc) · 1.03 KB
/
inheritance.py
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
# inheritance
class User():
def __init__(self, email):
self._email = email
def signed_in(self):
print('Logged in')
# encapsulation
class Wizard(User): # inheritance
def __init__(self, name, email, power):
super().__init__(email)
self._name = name # abstraction
self._power = power # abstraction
def attack(self): # polymorphism
print(f'Wizard {self._name} is attacking with a power of {self._power}')
class Archer(User):
def __init__(self, name, num_arrow):
self._name = name
self._num_arrows = num_arrow
def attack(self): # polymorphism
print(f'Archer {self._name} is attacking with {self._num_arrows} Arrows')
def char_player(char):
char.signed_in()
char.attack()
# instanciate
wiz1 = Wizard("merlin", "[email protected]", 20)
arch1 = Archer("robinhood", 100)
print(wiz1._email)
# polymorphism 1
char_player(wiz1)
char_player(arch1)
# polymorphism 2 using for loop
for char in [wiz1, arch1]:
char.signed_in()
char.attack()