-
Notifications
You must be signed in to change notification settings - Fork 0
Лабораторная работа 3 #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AlJoff
wants to merge
2
commits into
main
Choose a base branch
from
module2-lab3
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| class Book: | ||
| """ Базовый класс книги. """ | ||
|
|
||
| def __init__(self, name: str, author: str): | ||
| self._name = name | ||
| self._author = author | ||
|
|
||
| @property | ||
| def name(self) -> str: | ||
| return self._name | ||
|
|
||
| @property | ||
| def author(self) -> str: | ||
| return self._author | ||
|
|
||
| def __str__(self): | ||
| return f"Книга {self.name}. Автор {self.author}" | ||
|
|
||
| def __repr__(self): | ||
| return f"{self.__class__.__name__}(name={self.name!r}, author={self.author!r})" | ||
|
|
||
|
|
||
| class PaperBook(Book): | ||
| def __init__(self, name: str, author: str, pages: int): | ||
| super().__init__(name, author) | ||
| self.pages = pages | ||
|
|
||
| @property | ||
| def pages(self) -> int: | ||
| return self._pages | ||
|
|
||
| @pages.setter | ||
| def pages(self, pages: int): | ||
| if not isinstance(pages, int): | ||
| raise TypeError("Количество страниц должно быть типа int") | ||
| if pages < 1: | ||
| raise ValueError("Количество страниц должно быть положительным числом") | ||
| self._pages = pages | ||
|
|
||
| def __str__(self): | ||
| return f"{super().__str__()}. Страниц {self.pages}" | ||
|
|
||
| def __repr__(self): | ||
| return f"{super().__repr__()[:-1]}, pages={self.pages!r})" | ||
|
|
||
|
|
||
| class AudioBook(Book): | ||
| def __init__(self, name: str, author: str, duration: float): | ||
| super().__init__(name, author) | ||
| self.duration = duration | ||
|
|
||
| @property | ||
| def duration(self) -> float: | ||
| return self._duration | ||
|
|
||
| @duration.setter | ||
| def duration(self, duration: float): | ||
| if not isinstance(duration, float): | ||
| raise TypeError("Продолжительность аудиокниги должна быть типа float") | ||
| if duration <= 0: | ||
| raise ValueError("Продолжительность аудиокниги должна быть положительным числом") | ||
| self._duration = duration | ||
|
|
||
| def __str__(self): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Аналогично PaperBook |
||
| return f"{super().__str__()}. Продолжительность {self.duration}" | ||
|
|
||
| def __repr__(self): | ||
| return f"{super().__repr__()[:-1]}, duration={self.duration!r})" | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Здесь, точна та же информация что и в родительском классе Book Книга и автор. Не нужно перегружать этот метод
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Не совсем понял.
В методе помимо информации о книге и авторе из родительского класса Book выводится также информация о количестве страниц из самого класса PapperBook. Т.е. получается при перегрузке метода добавляет новая информация о количестве страниц в книге.
Аналогично и в AudioBook только там вместо количества страниц выводится продолжительность аудиокниги.
В любом случае я поправил код убрав перегрузку метода str из классов PapperBook и AudioBook
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Количество страниц или продолжительность книги характеризуют уже более конкретный объект - бумажную либо аудио-книгу. Эту информацию мы предоставляем именно в repr когда отображаем текстовое представление объекта, более общую суть единую для всех, а именно автора и название несёт в себе str