Advertisement
ksieradzinski

Untitled

Jun 5th, 2025
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. # "W pustyni i w puszczy", "Henryk Sienkiewicz"
  2.  
  3. class Author:
  4. def __init__(self, first_name: str, last_name: str):
  5. self.first_name = first_name
  6. self.last_name = last_name
  7.  
  8. def get_info(self):
  9. return f"{self.first_name} {self.last_name}"
  10.  
  11. class Category:
  12. def __init__(self, name):
  13. self.name = name
  14.  
  15. class Book:
  16. def __init__(self, title: str, author: Author, categories:list = None):
  17. self.title = title
  18. self.author = author
  19. self.categories = [] if categories is None else categories
  20.  
  21. def get_info(self):
  22. return f"Książka {self.title}, autorem tej książki jest {self.author.get_info()}"
  23.  
  24.  
  25. # Kompozycja
  26. # Zamiast rozbudowywać jedną klasę, łączymy ze sobą różne klasy poprzez obiekty
  27.  
  28. cat_1 = Category("Lektura")
  29. cat_2 = Category("Przygodowa")
  30.  
  31. henry = Author("Henryk", "Sienkiewicz")
  32. book = Book(title="W pustyni i w puszczy", author=henry, categories=[cat_1, cat_2])
  33. print(book.get_info())
  34.  
  35. sophia = Author("Zofia", "Nałkowska")
  36. book = Book(title="Granice", author=sophia, categories=[cat_1])
  37. print(book.get_info())
  38.  
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement