使用Python实现下述功能。 ??????
笔记(note)是存在笔记本(notebook)里的短文。每一篇笔记都应该记录下它被创建的时间,并且为了查询方便,添加从1开始自动编号的ID。可以查看笔记本中所有笔记。笔记应该可以修改和删除。我们也需要能够通过ID搜索笔记。
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
import time
class Notebook:
id_ = 0
__notes = []
def print_notes(self):
for note in self.__notes:
note.print_note()
def find_note_by_id(self, note_id):
for note in self.__notes:
if note.get_id() == note_id:
return note
def add_note(self, note):
self.__notes.append(note)
def update_note(self, note_id, article):
for n in self.__notes:
if n.get_id() == note_id:
n.set_article(article)
def del_note(self, note_id):
for n in self.__notes:
if n.get_id() == note_id:
Notebook.__notes.remove(n)
class Note:
def __init__(self, article):
Notebook.id_ += 1
self.__id = Notebook.id_
self.__article = article
self.__time = time.strftime("%Y-%m-%d")
def __str__(self):
return f"id: {self.__id}, article: {self.__article}, time: {self.__time}"
def get_id(self):
return self.__id
def get_article(self):
return self.__article
def set_article(self, article):
self.__article = article
def print_note(self):
print(self.__str__())
if __name__ == "__main__":
notebook = Notebook()
# 向笔记本中添加3条笔记
note1 = Note("今天我写了第1条笔记")
notebook.add_note(note1)
note2 = Note("今天我写了第2条笔记")
notebook.add_note(note2)
note3 = Note("今天我写了第3条笔记")
notebook.add_note(note3)
# 查看笔记本中所有笔记
notebook.print_notes()
# 查找第1篇笔记
note1_find = notebook.find_note_by_id(1)
print("-------")
note1_find.print_note()
# 修改第一篇笔记
notebook.update_note(1, "修改了第1条笔记")
# 删除第2篇笔记
notebook.del_note(2)
print("-------")
notebook.print_notes()
|