forked from abhaysamantni/Python_OOP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolymorphism_demo2.py
More file actions
36 lines (30 loc) · 891 Bytes
/
polymorphism_demo2.py
File metadata and controls
36 lines (30 loc) · 891 Bytes
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
# This program demonstrates polymorphism.
import animals
def main():
# Create an Mammal object, a Dog object, and
# a Cat object.
mammal = animals.Mammal('regular animal')
dog = animals.Dog()
cat = animals.Cat()
# Display information about each one.
print('Here are some animals and')
print('the sounds they make.')
print('--------------------------')
show_mammal_info(mammal)
print()
show_mammal_info(dog)
print()
show_mammal_info(cat)
print()
show_mammal_info('I am a string')
# The show_mammal_info function accepts an object
# as an argument, and calls its show_species
# and make_sound methods.
def show_mammal_info(creature):
if isinstance(creature, animals.Mammal):
creature.show_species()
creature.make_sound()
else:
print('That is not a Mammal!')
# Call the main function.
main()