Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Дз от 23.10.2024г.
- 1)
- import abc
- class Character(abc.ABC):
- @abc.abstractmethod
- def attack(self, target):
- pass
- @abc.abstractmethod
- def defend(self, damage):
- pass
- class Warrior(Character):
- def __init__(self, name, weapon):
- self.name = name
- self.weapon = weapon
- self.health = 100
- def attack(self, target):
- print(f"{self.name} атакует {target.name} с помощью оружия: {self.weapon.name}!")
- target.defend(self.weapon.damage)
- def defend(self, damage):
- print(f"{self.name} получает {damage} урона.")
- self.health -= damage
- print(f'Состояние здоровья воина: {self.health}')
- if self.health <= 0:
- print(f"{self.name} погиб!")
- class Mage(Character):
- def __init__(self, name, spell):
- self.name = name
- self.spell = spell
- self.health = 80
- def attack(self, target):
- print(f"{self.name} использует магическое заклинание {self.spell.name}!")
- target.defend(self.spell.damage)
- def defend(self, damage):
- print(f"{self.name} защищается барьером!") # (снижает получаемый урон на 50%)
- self.health -= damage * 0.5
- print(f'Состояние здоровья Мага: {self.health}')
- if self.health <= 0:
- print(f"{self.name} погиб!")
- class Weapon:
- def __init__(self, name, damage):
- self.name = name
- self.damage = damage
- class Spell:
- def __init__(self, name, damage):
- self.name = name
- self.damage = damage
- """Пример использования"""
- sword = Weapon("Катана", 20)
- fireball = Spell("fireball", 30)
- warrior = Warrior("Артур", sword)
- mage = Mage('Омникус', fireball)
- warrior.attack(mage)
- mage.attack(warrior)
- 2)
- import abc
- class Book:
- def __init__(self, title, author):
- self.title = title
- self.author = author
- @abc.abstractmethod
- def get_summary(self):
- pass
- class Fiction(Book):
- def get_summary(self):
- return f'{self.title} - это художественная книга, написанная автором: {self.author}'
- class NonFiction(Book):
- def get_summary(self):
- return f'{self.title} - это информативная книга, написанная автором: {self.author}'
- class Poetry(Book):
- def get_summary(self):
- return f'{self.title} - это сборник стихов от автора: {self.author}'
- """Пример использования с созданием списка книг"""
- books = [
- Fiction("Муму", "Иван Тургенев"),
- NonFiction("Бойцовский клуб", "Чак Паланик"),
- Poetry("Сборник стихотворений", "Александр Пушкин")
- ]
- for book in books:
- print(book.get_summary())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement