-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06_Animal.py
69 lines (52 loc) · 1.5 KB
/
06_Animal.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from abc import ABC, abstractmethod
# Abstract Class: Animal
class Animal(ABC):
def __init__(self, name):
self.name = name
@abstractmethod
def make_sound(self):
pass
# Abstract Class: Swimmer
class Swimmer(ABC):
@abstractmethod
def swim(self):
pass
# Concrete Class: Fish (inherits from Animal and Swimmer)
class Fish(Animal, Swimmer):
def make_sound(self):
return "Blub blub!"
def swim(self):
return f"{self.name} is swimming like a fish."
# Abstract Class: Flyer
class Flyer(ABC):
@abstractmethod
def fly(self):
pass
# Concrete Class: Bird (inherits from Animal and Flyer)
class Bird(Animal, Flyer):
def make_sound(self):
return "Tweet tweet!"
def fly(self):
return f"{self.name} is flying like a bird."
# Concrete Class: Bat (inherits from Animal, Swimmer, and Flyer)
class Bat(Animal, Swimmer, Flyer):
def make_sound(self):
return "Squeak squeak!"
def swim(self):
return f"{self.name} is swimming upside down like a bat."
def fly(self):
return f"{self.name} is flying like a bat."
# Creating instances of concrete classes
goldfish = Fish(name="Goldie")
robin = Bird(name="Robin")
fruit_bat = Bat(name="Fruity")
# Displaying information about animals with multiple inheritance
print(goldfish.make_sound())
print(goldfish.swim())
print("\n")
print(robin.make_sound())
print(robin.fly())
print("\n")
print(fruit_bat.make_sound())
print(fruit_bat.swim())
print(fruit_bat.fly())