-
Notifications
You must be signed in to change notification settings - Fork 8
/
08_polimorfizm.py
59 lines (40 loc) · 1.21 KB
/
08_polimorfizm.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
from abc import ABC, abstractmethod
class Ksztalt(ABC):
@abstractmethod
def pole(self):
pass
@abstractmethod
def obwod(self):
pass
class Kolo(Ksztalt):
def __init__(self, promien):
self.promien = promien
def obwod(self):
return 2 * 3.14 * self.promien
def pole(self):
return 3.14 * self.promien ** 2
def __str__(self):
return f"Kolo o promieniu {self.promien}"
def __repr__(self):
return self.__str__()
class Kwadrat(Ksztalt):
def __init__(self, bok):
self.bok = bok
def obwod(self):
return 4 * self.bok
def pole(self):
return self.bok ** 2
def __str__(self):
return f"Kwadrat o boku {self.bok}"
def __repr__(self):
return self.__str__()
if __name__ == "__main__":
# ksztalt = Ksztalt() # TypeError: Can't instantiate abstract class Ksztalt with abstract methods obwod, pole
kolo = Kolo(5)
kwadrat = Kwadrat(5)
ksztalty = [kolo, kwadrat]
for ksztalt in ksztalty:
print(ksztalt)
print(f"Pole: {ksztalt.pole()}")
print(f"Obwod: {ksztalt.obwod()}")
print()