TelenkovDmitry 8 luni în urmă
părinte
comite
fb6677fd3b

+ 128 - 0
courses/python_oop/inheritance/many.py

@@ -0,0 +1,128 @@
+# Множественное наследование
+# MRO
+
+class Doctor:
+
+    def can_cure(self):
+        print('Я доктор, я умею лечить')
+
+class Builder:
+
+    def can_build(self):
+        print('Я строитель, я умею строить')
+
+class Person(Doctor, Builder):
+    def can_build(self):
+        print('Я человек, я тоже умею строить')
+        Builder.can_build()
+
+# ----------------------------------------------------------
+
+class Product:
+
+    def __init__(self, name, price):
+        self.name = name
+        self.price = price
+
+    def get_price(self):
+        return self.price
+    
+
+class Discount():
+    
+    def apply_discount(self, discount):
+        self.price -= self.price * discount
+
+
+class DiscountProduct(Product, Discount):
+    pass
+
+# ----------------------------------------------------------
+
+class Mother:
+    
+    def __init__(self, mother_name):
+        self.mother_name = mother_name
+
+class Father:
+
+    def __init__(self, father_name):
+        self.father_name = father_name
+
+class Child(Mother, Father):
+    
+    def __init__(self, child_name, mother_name, father_name):
+        super().__init__(mother_name)
+        Father.__init__(self, father_name)
+        self.child_name = child_name
+
+    def introduce(self):
+        return f"Меня зовут {self.child_name}. Моя мама - {self.mother_name}, мой папа - {self.father_name}"
+
+
+# ----------------------------------------------------------
+
+class User:
+
+    def __init__(self, name, password):
+        self.name = name
+        self.password = password
+
+    def get_info(self):
+        return f"Имя пользователя: {self.name}"
+    
+class Authentication(User):
+
+    def authenticate(self, name, password):
+        return self.name == name and self.password == password
+
+
+class AuthenticatedUser(Authentication, User):
+    pass
+
+# ----------------------------------------------------------
+
+class Person:
+
+    def __init__(self, name, age):
+        self.name = name
+        self.age = age
+        
+    def display_person_info(self):
+        print(f"Person: {self.name}, {self.age}")
+
+class Company:
+
+    def __init__(self, company_name, location):
+        self.company_name = company_name
+        self.location = location
+
+    def display_company_info(self):
+        print(f"Company: {self.company_name}, {self.location}")
+
+class Employee(Person, Company):
+
+    def __init__(self, name, age, company_name, location):
+        super().__init__(name, age)
+        Company.__init__(self, company_name, location)
+
+
+
+
+def main():
+
+    emp = Employee('Jessica', 28, 'Google', 'Atlanta')
+    emp.display_person_info()
+    emp.display_company_info()
+
+    # s = Person()
+    # s.can_build()
+
+    # print(Person.__mro__)
+
+    # p1 = DiscountProduct('Телефон', 10000)
+    # p1.apply_discount(0.1)
+    # print(p1.get_price())
+
+if __name__ == '__main__':
+    main()

+ 29 - 0
courses/python_oop/inheritance/many_2.py

@@ -0,0 +1,29 @@
+
+class First(object):
+
+    def __init__(self):
+        print('First(): entering')
+        super(First, self).__init__()
+        print("First(): exiting")
+
+class Second(object):
+
+    def __init__(self):
+        print("Second(): entering")
+        super(Second, self).__init__()
+        print("Second(): exiting")
+
+class Third(First, Second):
+    
+    def __init__(self):
+        print("Third(): entering")
+        super(Third, self).__init__()
+        print("Third(): exiting")
+
+
+def main():
+    Third()
+
+
+if __name__ == '__main__':
+    main()