-
Notifications
You must be signed in to change notification settings - Fork 0
/
inheritance.py
49 lines (38 loc) · 1023 Bytes
/
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
class Animal:
def __init__(self, name='', health=0):
self.name = name
self.health = health
def walk(self):
self.health -= 1
return self
def run(self):
self.health -=5
return self
def display_health(self):
print('Animal\'s health:', self.health)
animal1 = Animal("yes", 50)
animal1.walk().walk().walk().run().run().display_health()
class Dog(Animal):
def __init__(self):
super().__init__()
self.health = 150
def pet(self):
self.health += 5
return self
dog1=Dog()
dog1.walk().walk().walk().run().run().pet().display_health()
class Dragon(Animal):
def __init__(self):
super().__init__()
self.health = 170
def fly(self):
self.health-=10
return self
def display_health(self):
super().display_health()
print('I am a Dragon')
return self
dragon1 = Dragon()
dragon1.display_health()
animal2 = Animal("animal2", 40)
animal2.display_health()