设计一个图书管理系统,
基类为类Book,要求有书名和作者属性,
由Book类派生子类AudioBook(有声书,需要具有演说者属性),
对于Book和AudioBook进行合理的属性及行为的抽象,
编写图书管理类用于管理图书以及有声图书
class Book:
def __init__(self, title, author):
self.title = title
self.author = author
def display_info(self):
print(f"书名: {self.title}, 作者: {self.author}")
class AudioBook(Book):
def __init__(self, title, author, narrator):
super().__init__(title, author) # 调用父类的构造器
self.narrator = narrator
def display_info(self):
# 重写父类的方法以包含演说者信息
print(f"书名: {self.title}, 作者: {self.author}, 演说者: {self.narrator}")
class Library:
def __init__(self):
self.books = [] # 存储所有图书的列表
def add_book(self, book):
self.books.append(book)
def display_all_books(self):
for book in self.books:
book.display_info()
# 示例使用
if __name__ == "__main__":
lib = Library()
lib.add_book(Book("Python编程", "张三"))
lib.add_book(AudioBook("Python编程有声版", "李四", "王五"))
lib.display_all_books()
标签:__,有声书,author,python,self,title,book,面向对象编程,def
From: https://blog.csdn.net/dudnf/article/details/140779107