many.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # Множественное наследование
  2. # MRO
  3. class Doctor:
  4. def can_cure(self):
  5. print('Я доктор, я умею лечить')
  6. class Builder:
  7. def can_build(self):
  8. print('Я строитель, я умею строить')
  9. class Person(Doctor, Builder):
  10. def can_build(self):
  11. print('Я человек, я тоже умею строить')
  12. Builder.can_build()
  13. # ----------------------------------------------------------
  14. class Product:
  15. def __init__(self, name, price):
  16. self.name = name
  17. self.price = price
  18. def get_price(self):
  19. return self.price
  20. class Discount():
  21. def apply_discount(self, discount):
  22. self.price -= self.price * discount
  23. class DiscountProduct(Product, Discount):
  24. pass
  25. # ----------------------------------------------------------
  26. class Mother:
  27. def __init__(self, mother_name):
  28. self.mother_name = mother_name
  29. class Father:
  30. def __init__(self, father_name):
  31. self.father_name = father_name
  32. class Child(Mother, Father):
  33. def __init__(self, child_name, mother_name, father_name):
  34. super().__init__(mother_name)
  35. Father.__init__(self, father_name)
  36. self.child_name = child_name
  37. def introduce(self):
  38. return f"Меня зовут {self.child_name}. Моя мама - {self.mother_name}, мой папа - {self.father_name}"
  39. # ----------------------------------------------------------
  40. class User:
  41. def __init__(self, name, password):
  42. self.name = name
  43. self.password = password
  44. def get_info(self):
  45. return f"Имя пользователя: {self.name}"
  46. class Authentication(User):
  47. def authenticate(self, name, password):
  48. return self.name == name and self.password == password
  49. class AuthenticatedUser(Authentication, User):
  50. pass
  51. # ----------------------------------------------------------
  52. class Person:
  53. def __init__(self, name, age):
  54. self.name = name
  55. self.age = age
  56. def display_person_info(self):
  57. print(f"Person: {self.name}, {self.age}")
  58. class Company:
  59. def __init__(self, company_name, location):
  60. self.company_name = company_name
  61. self.location = location
  62. def display_company_info(self):
  63. print(f"Company: {self.company_name}, {self.location}")
  64. class Employee(Person, Company):
  65. def __init__(self, name, age, company_name, location):
  66. super().__init__(name, age)
  67. Company.__init__(self, company_name, location)
  68. def main():
  69. emp = Employee('Jessica', 28, 'Google', 'Atlanta')
  70. emp.display_person_info()
  71. emp.display_company_info()
  72. # s = Person()
  73. # s.can_build()
  74. # print(Person.__mro__)
  75. # p1 = DiscountProduct('Телефон', 10000)
  76. # p1.apply_discount(0.1)
  77. # print(p1.get_price())
  78. if __name__ == '__main__':
  79. main()