Last active
June 21, 2026 12:58
-
-
Save sunmeat/c8d56d4cf6dc862240296430b91042c4 to your computer and use it in GitHub Desktop.
aggregation example python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Hat: | |
| def __init__(self, color="чорний", model="класичний", price=99.99): | |
| self.color = color | |
| self.model = model | |
| self.price = price | |
| def __str__(self): | |
| return f"капелюх {self.model} кольору {self.color} за {self.price} грн" | |
| class Person: | |
| def __init__(self, name): | |
| self.name = name | |
| self.hat: Hat = None # !!! агрегування !!! спочатку без капелюха | |
| def take_hat(self, hat: Hat): | |
| self.hat = hat | |
| print(f"{self.name} надів {hat}") | |
| def go_walk(self): | |
| print(f"{self.name} пішов гуляти...") | |
| if self.hat: | |
| print("Раптово подув вітер... і капелюх полетів!") | |
| self.hat = None # втрачаємо капелюх (але він не знищується) | |
| else: | |
| print("А капелюха й не було...") | |
| def where_is_your_hat(self): | |
| if self.hat: | |
| print(f"Ось він! {self.hat}") | |
| else: | |
| print("Капелюха немає :(") | |
| if __name__ == "__main__": | |
| nice_hat = Hat(color="сірий", model="фетровий", price=1799.0) | |
| alex = Person("Олександр") | |
| alex.take_hat(nice_hat) | |
| alex.where_is_your_hat() | |
| alex.go_walk() | |
| alex.where_is_your_hat() | |
| print(f"Але сам капелюх все ще існує: {nice_hat}") | |
| print("Він може полетіти до когось іншого :)") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment