python编写程序完成以下要求:定义图书馆类Library、图书类Book、学生类Student。 其中,图书馆类Library包含私有属性:图书总量和馆藏图书,并包含以下方法的定义: ①初始化方法:init(self),用于设置私有属性图书总量为0、馆藏图书为空字典{}。 ②获取图书总量:getTotalAmount (self),返回值为整数。 ③获取可借图书列表:getAvailable (self),返回值为以book对象为元素的列表。 ④添加图书:add (self, *books),没有返回
时间: 2024-03-25 08:42:10 浏览: 26
值,参数books为可变参数,表示要添加的图书,每个图书对象为Book类的实例。 ⑤删除图书:delete (self, *books),没有返回值,参数books同上,表示要删除的图书。 ⑥借书:lend (self, book, student),没有返回值,参数book为Book类的实例,student为Student类的实例,表示借阅图书的学生。 ⑦还书:returnBook (self, book),没有返回值,参数book同上,表示归还的图书。
图书类Book包含私有属性:编号、书名、作者和借阅状态(默认为False,表示未借出)。包含以下方法的定义: ①初始化方法:init(self, id, name, author),参数分别为编号、书名、作者。 ②获取书名:getName(self),返回值为字符串。 ③获取作者:getAuthor(self),返回值为字符串。 ④获取编号:getId(self),返回值为整数。 ⑤获取借阅状态:isBorrowed(self),返回值为布尔值。 ⑥修改借阅状态:setBorrowed(self, status),没有返回值,参数status为布尔值,表示借阅状态。
学生类Student包含私有属性:姓名、学号和已借图书列表。包含以下方法的定义: ①初始化方法:init(self, name, id),参数分别为姓名和学号。 ②获取姓名:getName(self),返回值为字符串。 ③获取学号:getId(self),返回值为整数。 ④获取已借图书列表:getBorrowedBooks(self),返回值为以Book类对象为元素的列表。 ⑤添加已借图书:addBorrowedBook(self, book),没有返回值,参数book为Book类的实例。 ⑥删除已借图书:deleteBorrowedBook(self, book),没有返回值,参数book同上,表示要删除的已借图书。
下面是Python代码实现:
```python
class Book:
def __init__(self, id, name, author):
self.__id = id
self.__name = name
self.__author = author
self.__isBorrowed = False
def getName(self):
return self.__name
def getAuthor(self):
return self.__author
def getId(self):
return self.__id
def isBorrowed(self):
return self.__isBorrowed
def setBorrowed(self, status):
self.__isBorrowed = status
class Student:
def __init__(self, name, id):
self.__name = name
self.__id = id
self.__borrowedBooks = []
def getName(self):
return self.__name
def getId(self):
return self.__id
def getBorrowedBooks(self):
return self.__borrowedBooks
def addBorrowedBook(self, book):
self.__borrowedBooks.append(book)
def deleteBorrowedBook(self, book):
self.__borrowedBooks.remove(book)
class Library:
def __init__(self):
self.__totalAmount = 0
self.__books = {}
def getTotalAmount(self):
return self.__totalAmount
def getAvailable(self):
return [book for book in self.__books.values() if not book.isBorrowed()]
def add(self, *books):
for book in books:
self.__books[book.getId()] = book
self.__totalAmount += 1
def delete(self, *books):
for book in books:
del self.__books[book.getId()]
self.__totalAmount -= 1
def lend(self, book, student):
if book.isBorrowed():
print("该书已被借出")
else:
book.setBorrowed(True)
student.addBorrowedBook(book)
def returnBook(self, book):
if book.isBorrowed():
book.setBorrowed(False)
for student in self.__students:
if book in student.getBorrowedBooks():
student.deleteBorrowedBook(book)
break
else:
print("该书未被借出")
```
使用示例:
```python
book1 = Book(1, "Python入门教程", "张三")
book2 = Book(2, "Java入门教程", "李四")
book3 = Book(3, "C++入门教程", "王五")
stu1 = Student("小明", 1001)
stu2 = Student("小红", 1002)
lib = Library()
lib.add(book1, book2, book3)
lib.lend(book1, stu1)
print(stu1.getBorrowedBooks())
print(lib.getAvailable())
lib.returnBook(book1)
print(stu1.getBorrowedBooks())
print(lib.getAvailable())
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)