-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExercise04.py
73 lines (51 loc) · 1.42 KB
/
Exercise04.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
70
71
72
73
from abc import ABC, abstractmethod
# interface
class pet(ABC): # interface
@abstractmethod
def play(self) -> None:
pass
@abstractmethod
def set_name(self, name: str) -> None:
pass
@abstractmethod
def get_name(self) -> str:
pass
# abstract class
class animal(ABC):
def __init__(self, legs: int) -> None:
self.__legs = legs
@property
def legs(self) -> int:
return self.__legs
# concrete method
def walk(self) -> None:
print(f"Animal with {self.legs} legs is walking now...")
@abstractmethod
def eat(self) -> None:
pass
class spider(animal):
def __init__(self):
super().__init__(8)
def eat(self) -> None:
print("Spider is eating now...")
class cat(animal, pet):
def __init__(self, name: str = "Tekir"):
super().__init__(4)
self.__name = name
@property
def name(self) -> str:
return self.__name
def eat(self) -> None:
print(f"{self.name} is eating now...")
def play(self) -> None:
print(f"{self.name} is playing now...")
def set_name(self, name: str) -> None:
self.__name = name
def get_name(self) -> str:
return self.name
zoo_animals: list[animal] = [spider(), cat("Garfield"), cat(), spider()]
for a_animal in zoo_animals:
a_animal.walk()
a_animal.eat()
if isinstance(a_animal, pet):
a_animal.play()