Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # "W pustyni i w puszczy", "Henryk Sienkiewicz"
- class Author:
- def __init__(self, first_name: str, last_name: str):
- self.first_name = first_name
- self.last_name = last_name
- def get_info(self):
- return f"{self.first_name} {self.last_name}"
- class Category:
- def __init__(self, name):
- self.name = name
- class Book:
- def __init__(self, title: str, author: Author, categories:list = None):
- self.title = title
- self.author = author
- self.categories = [] if categories is None else categories
- def get_info(self):
- return f"Książka {self.title}, autorem tej książki jest {self.author.get_info()}"
- # Kompozycja
- # Zamiast rozbudowywać jedną klasę, łączymy ze sobą różne klasy poprzez obiekty
- cat_1 = Category("Lektura")
- cat_2 = Category("Przygodowa")
- henry = Author("Henryk", "Sienkiewicz")
- book = Book(title="W pustyni i w puszczy", author=henry, categories=[cat_1, cat_2])
- print(book.get_info())
- sophia = Author("Zofia", "Nałkowska")
- book = Book(title="Granice", author=sophia, categories=[cat_1])
- print(book.get_info())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement