abdrasctractclass.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # https://metanit.com/python/tutorial/7.8.php
  2. import abc, math
  3. class Shape(abc.ABC):
  4. """
  5. aбстрактный класс геометрической фигуры
  6. :param x: - абсцисса центра
  7. :param y: - ордината центра
  8. """
  9. def __init__(self, x, y):
  10. self.x = x
  11. self.y = y
  12. @abc.abstractmethod
  13. def area (self): pass # абстрактны метод
  14. def print_point(self):
  15. '''
  16. неабстрактный метод возвращает
  17. координаты центра фигуры
  18. '''
  19. print(f"X: {self.x}, \tY: {self.y}")
  20. class Rectangle(Shape):
  21. '''
  22. класс прямоугольника
  23. '''
  24. def __init__(self, x, y, width, height):
  25. super().__init__(x, y)
  26. self.width = width
  27. self.height = height
  28. def area (self): return self.width * self.height
  29. class Circle(Shape):
  30. '''
  31. класс круга
  32. '''
  33. def __init__(self, xCenter, yCenter, radius):
  34. super().__init__(xCenter, yCenter)
  35. self.radius = radius
  36. def area (self): return self.radius * self.radius * math.pi
  37. def print_area(figure):
  38. print(f"Area: {figure.area():.2f}")
  39. if __name__ == '__main__':
  40. rect = Rectangle(10, 20, 100, 100)
  41. rect.print_point() # X: 10 Y: 20
  42. circle = Circle(0, 0, 30)
  43. circle.print_point() # X: 0, Y: 0
  44. print_area(rect) # Area: 10000.00
  45. print_area(circle) # Area: 2827.43