24 lines
686 B
Python
24 lines
686 B
Python
class Flyable:
|
|
def fly(self):
|
|
return "Flying high!"
|
|
|
|
class Swimmable:
|
|
def swim(self):
|
|
return "Swimming deep!"
|
|
|
|
class Duck(Flyable, Swimmable):
|
|
def __init__(self, name):
|
|
self.name = name
|
|
|
|
d = Duck("Donald")
|
|
print(f"{d.name} is {d.fly()} and {d.swim()}")
|
|
|
|
# Output:
|
|
# Donald is Flying high! and Swimming deep!
|
|
|
|
# Demonstrates multiple inheritance where Duck inherits from both Flyable and Swimmable classes.
|
|
# The Duck class can use methods from both parent classes.
|
|
# The order of inheritance matters; methods are resolved in the order of the base classes listed.
|
|
# In this case, Flyable is checked before Swimmable for method resolution.
|
|
|