class Book: def __init__(self, title, author, isbn, available=True): self.title = title self.author = author self.isbn = isbn self.available = availableclass Library: def __init__(self, books=None): if books is None: books = [] self.books = books def add_book(self, book): self.books.append(book) def remove_book(self, book): self.books.remove(book) def search_book_by_title(self, title): return [book for book in self.books if book.title == title] def search_book_by_author(self, author): return [book for book in self.books if book.author == author] def search_book_by_isbn(self, isbn): return [book for book in self.books if book.isbn == isbn] def checkout_book(self, book): if book.available: book.available = False return True else: return False def return_book(self, book): book.available = Trueif __name__ == '__main__': library = Library() book1 = Book('The Great Gatsby', 'F. Scott Fitzgerald', '978-0143129453') book2 = Book('To Kill a Mockingbird', 'Harper Lee', '978-0446310789') book3 = Book('1984', 'George Orwell', '978-0451524935') library.add_book(book1) library.add_book(book2) library.add_book(book3) print(library.search_book_by_title('1984'))使用有什么要求吗
时间: 2023-12-06 20:42:45 浏览: 121
python基础进阶1.6:面向对象之类,对象及__init__(),self相关用法讲解
这段代码是一个简单的图书馆管理系统,其中Book类表示一本书,而Library类表示整个图书馆。这个系统可以执行添加、删除、搜索、借阅和归还图书等操作。
使用此代码时,需要确保在程序运行前已经安装了Python编程语言的环境。并且需要在代码中添加更多的功能,比如用户登录、借阅记录、借阅期限等等。也需要进行更加完善的异常处理,以避免程序崩溃或者出现意外情况。
阅读全文