TelenkovDmitry 8 月之前
父節點
當前提交
0861dc2716
共有 2 個文件被更改,包括 161 次插入0 次删除
  1. 53 0
      courses/python_oop/inheritance/mixin.py
  2. 108 0
      courses/python_oop/inheritance/mixin_2.py

+ 53 - 0
courses/python_oop/inheritance/mixin.py

@@ -0,0 +1,53 @@
+
+# --------------------------------------------------
+# Миксин для преобразования к строке
+
+class ToStringMixin:
+    def __str__(self):
+        return f"{self.__class__.__name__}({str(self.__dict__)})"
+
+class MyClass(ToStringMixin):
+    def __init__(self, x, y):
+        self.x = x
+        self.y = y
+def main():
+    obj = MyClass(1, 2)
+    print(obj)
+
+if __name__ == '__main__':
+    main()
+
+
+# --------------------------------------------------
+# Миксин для подстчета количества экземпляров
+
+class CountInstancesMixin:
+    count = 0
+
+    def __init__(self, *args, **kwargs):
+        super().__init__(*args, **kwargs)
+        self.__class__.count += 1
+
+class Cat(CountInstancesMixin):
+    def __init__(self, name):
+        self.name = name
+        super().__init__()
+
+class Dog(CountInstancesMixin):
+    def __init__(self, name):
+        super().__init__()
+
+
+def main():
+    o1 = Cat('qwe')
+    o2 = Cat('ewer')
+
+    o3 = Dog('sdf')
+
+    print(Cat.count)
+    print(Dog.count)
+
+
+
+if __name__ == '__main__':
+    main()

+ 108 - 0
courses/python_oop/inheritance/mixin_2.py

@@ -0,0 +1,108 @@
+class BasePizza:
+    BASE_PIZZA_PRICE = 15
+
+    def __init__(self, name, price):
+        self.name = name
+        self.price = price
+        self.toppings = ['cheese']
+
+    def __str__(self):
+        return f"{self.name} with {self.toppings}, ${self.price:.2f}"
+
+
+class PepperoniMixin:
+    def add_pepperoni(self):
+        print("Adding pepperoni!")
+        self.price += 5
+        self.toppings += ['pepperoni']
+
+
+class MushroomMixin:
+    def add_mushrooms(self):
+        print("Adding mushrooms!")
+        self.price += 3
+        self.toppings += ['mushrooms']
+
+
+class OnionMixin:
+    def add_onion(self):
+        print("Adding onion!")
+        self.price += 2
+        self.toppings += ['onion']
+
+
+class BaconMixin:
+    def add_bacon(self):
+        print("Adding bacon!")
+        self.price += 6
+        self.toppings += ['bacon']
+
+
+class OlivesMixin:
+    def add_olives(self):
+        print("Adding olives!")
+        self.price += 1
+        self.toppings += ['olives']
+
+class HamMixin:
+    def add_ham(self):
+        print("Adding ham!")
+        self.price += 7
+        self.toppings += ['ham']
+
+class PepperMixin:
+    def add_pepper(self):
+        print("Adding pepper!")
+        self.price += 3
+        self.toppings += ['pepper']
+
+class ChickenMixin:
+    def add_chicken(self):
+        print("Adding chicken!")
+        self.price += 5
+        self.toppings += ['chicken']
+
+class OlivesPizza(BasePizza, OlivesMixin):
+
+    def __init__(self):
+        super().__init__('Море оливок', BasePizza.BASE_PIZZA_PRICE)
+        self.add_olives()
+
+
+class PepperoniPizza(BasePizza, PepperoniMixin):
+
+    def __init__(self):
+        super().__init__('Колбасятина', BasePizza.BASE_PIZZA_PRICE)
+        self.add_pepperoni()
+
+
+class MushroomOnionBaconPizza(BasePizza, MushroomMixin, OnionMixin, BaconMixin):
+
+    def __init__(self):
+        super().__init__('Грибной пяточок с луком', BasePizza.BASE_PIZZA_PRICE)
+        self.add_mushrooms()
+        self.add_onion()
+        self.add_bacon()
+
+class CountryPizza(BasePizza, HamMixin, PepperMixin, OlivesMixin, PepperoniMixin, MushroomMixin, ChickenMixin):
+
+    def __init__(self):
+        super().__init__('Деревенская пицца', BasePizza.BASE_PIZZA_PRICE)
+        self.add_ham()
+        self.add_pepper()
+        self.add_olives()
+        self.add_pepperoni()
+        self.add_mushrooms()
+        self.add_chicken()
+
+
+# --------------------------------------------------
+
+
+
+def main():
+    pass
+
+
+if __name__ == '__main__':
+    main()