Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Приклад Наслідування
- class A:
- pass
- class B:
- pass
- class C(A, B):
- pass
- # Приклад Реалізації / Імплементації
- from typing import Iterable, Callable
- class A:
- def __call__(self):
- return "foo"
- def __iter__(self):
- return iter([1, 2, 3])
- item = A()
- print(item()) # call
- print([i for i in item]) # iterate
- print(isinstance(item, Callable)) # True
- print(isinstance(item, Iterable)) # True
- # Приклад Залежності
- class Item:
- a = 123
- b = "abc"
- class Main:
- def process(self, item: Item):
- print(item.a)
- print(item.b)
- def create(self) -> Item:
- return Item()
- # Приклад Агрегації
- class Component:
- def do_something(self):
- print("I am helping")
- class Main:
- def __init__(self, component: Component):
- self.component = component
- def work(self):
- self.component.do_something()
- # Приклад Композиції
- class Component:
- def do_something(self):
- print("I am helping")
- class Main:
- def __init__(self):
- self.component = Component()
- def work(self):
- self.component.do_something()
- # Приклад Асоціації
- class Component:
- def __init__(self, main):
- self.main = main
- class Main:
- component = None
- def set_components(self, components: list[Component]):
- self.components = components
- def get_components(self) -> List[Components]:
- return self.components
- main = Main()
- components = [Component(main), Component(main), Component(main)]
- main.set_components(components)
- main.get_components()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement