Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions cartoon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from film import Film
from decorators import show_decorator

class Cartoon(Film):
# Перевизначаємо метод cost для мультфільму з декоратором
@show_decorator
def cost(self):
return self.duration * 25 + self.num_actors * 10
6 changes: 6 additions & 0 deletions category.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Category:
def __init__(self, category_name="Uncategorized"):
self.category_name = category_name

def describe(self):
return f"Категорія: {self.category_name}"
8 changes: 8 additions & 0 deletions decorators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Декоратор для методів
def show_decorator(func):
def wrapper(*args, **kwargs):
print(f"Викликається метод: {func.__name__}")
result = func(*args, **kwargs)
print(f"Метод {func.__name__} завершився")
return result
return wrapper
39 changes: 39 additions & 0 deletions film.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from decorators import show_decorator

class Film:
# Атрибут класу (наприклад, курс долара)
exchange_rate = 1.0 # Статичний атрибут класу

# Конструктор з параметрами
def __init__(self, title="Невідомий фільм", director="Невідомий режисер", duration=0, num_actors=0):
self._title = title # Приватний атрибут
self._director = director
self.duration = duration
self.num_actors = num_actors

# Властивість для назви фільму
@property
def title(self):
return self._title

@title.setter
def title(self, value):
if len(value) > 0:
self._title = value

# Метод для розрахунку вартості з декоратором
@show_decorator
def cost(self):
cost = self.duration * 20 + self.num_actors * 30
if self._director in ["Стівен Спілберг", "Джеймс Кемерон"]:
cost *= 2
return cost * Film.exchange_rate # Використовуємо статичний атрибут

# Метод __str__ для строкового подання
def __str__(self):
return f"Фільм: {self.title}, Режисер: {self._director}, Тривалість: {self.duration} хв., Акторів: {self.num_actors}"

# Статичний метод для зміни курсу
@staticmethod
def set_exchange_rate(rate):
Film.exchange_rate = rate
25 changes: 25 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from film import Film
from cartoon import Cartoon
from special_film import SpecialFilm

# Демонстраційна програма
def demo():
# Створюємо екземпляр класу Фільм
film = Film("Jurassic Park", "Стівен Спілберг", 127, 15)
print(film)
print(f"Вартість фільму: {film.cost()} тис. доларів")

# Створюємо екземпляр класу Мультфільм
cartoon = Cartoon("Toy Story", "Джон Лассетер", 81, 8)
print(cartoon)
print(f"Вартість мультфільму: {cartoon.cost()} тис. доларів")

# Створюємо екземпляр класу SpecialFilm (множинне наслідування)
special_film = SpecialFilm("Avatar", "Джеймс Кемерон", 162, 30, "Наукова фантастика")
print(special_film)
print(f"Вартість спеціального фільму: {special_film.cost()} тис. доларів")


# Запуск демонстрації
if __name__ == "__main__":
demo()
12 changes: 12 additions & 0 deletions special_film.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from film import Film
from category import Category

class SpecialFilm(Film, Category):
def __init__(self, title, director, duration, num_actors, category_name):
# Викликаємо конструктори обох батьківських класів
Film.__init__(self, title, director, duration, num_actors)
Category.__init__(self, category_name)

# Перевизначаємо метод __str__, щоб включити інформацію про категорію
def __str__(self):
return f"{super().__str__()}, {self.describe()}"