简单的小说书架管理代码
时间: 2023-07-03 22:27:59 浏览: 44
以下是一个简单的小说书架管理代码示例,可以作为参考:
```python
class Novel:
def __init__(self, name, author, category):
self.name = name
self.author = author
self.category = category
self.read_progress = 0
class Bookshelf:
def __init__(self, name, location):
self.name = name
self.location = location
self.novels = []
def add_novel(self, novel):
self.novels.append(novel)
def remove_novel(self, novel):
self.novels.remove(novel)
def edit_novel_info(self, novel, name=None, author=None, category=None):
if name:
novel.name = name
if author:
novel.author = author
if category:
novel.category = category
def search_novel(self, keyword):
result = []
for novel in self.novels:
if keyword in novel.name or keyword in novel.author or keyword in novel.category:
result.append(novel)
return result
def show_novels(self):
for novel in self.novels:
print(f"小说名:{novel.name},作者:{novel.author},分类:{novel.category},阅读进度:{novel.read_progress}")
```
以上代码定义了两个类:`Novel` 表示小说,包含小说的名称、作者、分类和阅读进度等属性;`Bookshelf` 表示书架,包含书架的名称、位置和小说列表等属性,同时还定义了添加小说、删除小说、编辑小说信息、搜索小说和展示小说列表等方法。
可以根据需要进行适当的修改和扩展,例如添加分类管理、阅读记录、排行榜等功能。