poly.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. # Полиморфизм
  2. # Проблема
  3. from functools import total_ordering
  4. class Rectangle:
  5. def __init__(self, a, b):
  6. self.a = a
  7. self.b = b
  8. def get_area(self):
  9. return self.a * self.b
  10. class Square:
  11. def __init__(self, a) -> None:
  12. self.a = a
  13. def get_area(self):
  14. return self.a**2
  15. class Circle:
  16. def __init__(self, r):
  17. self.r = r
  18. def get_area(self):
  19. return 3.14*self.r**2
  20. class UnitedKingdom:
  21. def __init__(self) -> None:
  22. pass
  23. @staticmethod
  24. def get_capital():
  25. print("London is the capital of Great Britain.")
  26. @staticmethod
  27. def get_language():
  28. print("English is the primary language of Great Britain.")
  29. class Spain:
  30. def __init__(self):
  31. pass
  32. @staticmethod
  33. def get_capital():
  34. print("Madrid is the capital of Spain.")
  35. @staticmethod
  36. def get_language():
  37. print("Spanish is the primary language of Spain.")
  38. class DateUSA:
  39. def __init__(self, day, month, year) -> None:
  40. self.day = day
  41. self.month = month
  42. self.year = year
  43. def format(self):
  44. return f"{self.month:02}/{self.day:02}/{self.year:04}"
  45. def isoformat(self):
  46. return f"{self.year:04}-{self.month:02}-{self.day:02}"
  47. class DateEurope:
  48. def __init__(self, day, month, year):
  49. self.day = day
  50. self.month = month
  51. self.year = year
  52. def format(self):
  53. return f"{self.day:02}/{self.month:02}/{self.year:02}"
  54. def isoformat(self):
  55. return f"{self.year:04}-{self.month:02}-{self.day:02}"
  56. @total_ordering
  57. class BankAccount:
  58. def __init__(self, name, balance):
  59. self.name = name
  60. self.balance = balance
  61. def __str__(self):
  62. return self.name
  63. def __len__(self):
  64. return len(str(self))
  65. def __lt__(self, other):
  66. if isinstance(other, BankAccount):
  67. return self.balance < other.balance
  68. elif isinstance(other, int):
  69. return self.balance < other
  70. else:
  71. raise ValueError
  72. def __add__(self, other):
  73. if isinstance(other, BankAccount):
  74. return self.balance + other.balance
  75. elif isinstance(other, Numbers):
  76. return self.balance + sum(other._values)
  77. elif isinstance(other, (int, float)):
  78. return self.balance + other
  79. else:
  80. raise ValueError
  81. def __radd__(self, other):
  82. return self.__add__(other)
  83. class Numbers:
  84. def __init__(self, values: list):
  85. self._values = values
  86. def __add__(self, other):
  87. if isinstance(other, Numbers):
  88. return sum(other._values) + sum(other._values)
  89. elif isinstance(other, BankAccount):
  90. return sum(self._values) + other.balance
  91. elif isinstance(other, (int, float)):
  92. return sum(self._values) + other
  93. else:
  94. raise ValueError
  95. def __radd__(self, other):
  96. return self.__add__(other)
  97. def main():
  98. lst = [4, BankAccount('Petr', 100), 5]
  99. print(sum(lst))
  100. # usa = DateUSA(1, 3, 2016)
  101. # print(usa.format())
  102. '''
  103. rect1 = Rectangle(3, 4)
  104. rect2 = Rectangle(12, 5)
  105. sq1 = Square(4)
  106. sq2 = Square(5)
  107. figures = [rect1, rect2, sq1, sq2]
  108. for figure in figures:
  109. print(figure.get_area())
  110. '''
  111. if __name__ == '__main__':
  112. main()