用Python面向对象写出图书管理系统
时间: 2023-02-27 20:03:29 浏览: 128
flask_pythonflask_flask书籍_图书管理系统_
5星 · 资源好评率100%
下面是用面向对象思想写出图书管理系统的一种方法:
```
class Book:
def __init__(self, title, author):
self.title = title
self.author = author
self.borrowed = False
def borrow(self):
if not self.borrowed:
self.borrowed = True
print("借阅成功!")
else:
print("该书已经被借走!")
def return_book(self):
if self.borrowed:
self.borrowed = False
print("还书成功!")
else:
print("该书没有被借走!")
class Library:
def __init__(self):
self.books = []
def add_book(self, book):
self.books.append(book)
print("成功添加图书:", book.title)
def list_books(self):
for book in self.books:
print("书名:", book.title, " 作者:", book.author, " 状态:", "已借出" if book.borrowed else "未借出")
def borrow_book(self, title):
for book in self.books:
if book.title == title:
book.borrow()
break
else:
print("该图书不存在!")
def return_book(self, title):
for book in self.books:
if book.title == title:
book.return_book()
break
else:
print("该图书不存在!")
```
这里的图书管理系统可以实现的功能有:添加图书、列出图书列表、借阅图书、还书。
阅读全文