Demo of dynamic polymorphism
This commit is contained in:
parent
6e52dba27b
commit
490d1aac42
15
dynamic_polymorphism.py
Normal file
15
dynamic_polymorphism.py
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
class Dog:
|
||||||
|
def speak(self):
|
||||||
|
return "Woof!"
|
||||||
|
|
||||||
|
class Cat:
|
||||||
|
def speak(self):
|
||||||
|
return "Meow!"
|
||||||
|
|
||||||
|
def animal_sound(animal):
|
||||||
|
return (animal.speak())
|
||||||
|
|
||||||
|
print(f" My dog says, {animal_sound(Dog())}") # Woof!
|
||||||
|
print(f" My cat says, {animal_sound(Cat())}") # Meow!
|
||||||
|
|
||||||
|
|
||||||
Loading…
Reference in New Issue
Block a user