123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- # Полиморфизм
- # Проблема
- from functools import total_ordering
- class Rectangle:
- def __init__(self, a, b):
- self.a = a
- self.b = b
- def get_area(self):
- return self.a * self.b
-
- class Square:
- def __init__(self, a) -> None:
- self.a = a
- def get_area(self):
- return self.a**2
- class Circle:
- def __init__(self, r):
- self.r = r
- def get_area(self):
- return 3.14*self.r**2
-
- class UnitedKingdom:
- def __init__(self) -> None:
- pass
- @staticmethod
- def get_capital():
- print("London is the capital of Great Britain.")
- @staticmethod
- def get_language():
- print("English is the primary language of Great Britain.")
- class Spain:
- def __init__(self):
- pass
- @staticmethod
- def get_capital():
- print("Madrid is the capital of Spain.")
- @staticmethod
- def get_language():
- print("Spanish is the primary language of Spain.")
- class DateUSA:
- def __init__(self, day, month, year) -> None:
- self.day = day
- self.month = month
- self.year = year
- def format(self):
- return f"{self.month:02}/{self.day:02}/{self.year:04}"
-
- def isoformat(self):
- return f"{self.year:04}-{self.month:02}-{self.day:02}"
- class DateEurope:
-
- def __init__(self, day, month, year):
- self.day = day
- self.month = month
- self.year = year
- def format(self):
- return f"{self.day:02}/{self.month:02}/{self.year:02}"
-
- def isoformat(self):
- return f"{self.year:04}-{self.month:02}-{self.day:02}"
- @total_ordering
- class BankAccount:
- def __init__(self, name, balance):
- self.name = name
- self.balance = balance
- def __str__(self):
- return self.name
-
- def __len__(self):
- return len(str(self))
- def __lt__(self, other):
- if isinstance(other, BankAccount):
- return self.balance < other.balance
- elif isinstance(other, int):
- return self.balance < other
- else:
- raise ValueError
- def __add__(self, other):
- if isinstance(other, BankAccount):
- return self.balance + other.balance
- elif isinstance(other, Numbers):
- return self.balance + sum(other._values)
- elif isinstance(other, (int, float)):
- return self.balance + other
- else:
- raise ValueError
- def __radd__(self, other):
- return self.__add__(other)
- class Numbers:
- def __init__(self, values: list):
- self._values = values
- def __add__(self, other):
- if isinstance(other, Numbers):
- return sum(other._values) + sum(other._values)
- elif isinstance(other, BankAccount):
- return sum(self._values) + other.balance
- elif isinstance(other, (int, float)):
- return sum(self._values) + other
- else:
- raise ValueError
-
- def __radd__(self, other):
- return self.__add__(other)
-
- def main():
- lst = [4, BankAccount('Petr', 100), 5]
- print(sum(lst))
- # usa = DateUSA(1, 3, 2016)
- # print(usa.format())
- '''
- rect1 = Rectangle(3, 4)
- rect2 = Rectangle(12, 5)
- sq1 = Square(4)
- sq2 = Square(5)
- figures = [rect1, rect2, sq1, sq2]
- for figure in figures:
- print(figure.get_area())
- '''
- if __name__ == '__main__':
- main()
|