-1
class EditorState:
    def __init__(self, content):
        self.content = content

class Editor:
    def __init__(self):
        self.content = ""

    def __str__(self):
        return f'{self.content}'

    def setContent(self, value):
        self.content = value

    def createContent(self):
        return EditorState(self.content)

    def restore(self, new_value):
        self.content = new_value

    def getcontent(self):
        return self.content

class History:
    def __init__(self):
        self.history = []

    def __repr__(self):
        return self.history

    def push(self, value):
        self.history.append(value)

    def remove(self):
        my_list = self.history
        my_list.pop()

        last_index = my_list[-1]
        return last_index

    def getvalue(self):
        my_list = self.history
        return self.history

editor = Editor()
history = History()

editor.setContent("a")
history.push(editor.createContent())

editor.setContent("b")
history.push(editor.createContent())

editor.setContent("c")
history.push(editor.createContent())

editor.setContent("D")
history.push(editor.createContent())

editor.restore(history.remove())

print(history.getvalue())
print(editor.getcontent())

当我检查列表中的项目时得到的输出:[< main .EditorState object at 0x0000017B77360040>, < main .EditorState object at 0x0000017B773600D0>, < main .EditorState object at 0x0000017B77360130>]

我想要的输出:[a,b,c]

我已经学会了如何在 java 中使用 Memento 模式,我想用 python 尝试这个模式。我确实工作,但问题是,当我从历史记录类的列表中返回最后一项时,它一直向我显示它的 id 而不是值。当我使用 getvalue() 方法打印列表时也是如此。

我尝试使用魔术方法 sush 作为strrepr但它不起作用,我也尝试将属性设置为变量但没有结果。

4

1 回答 1

0

解决它 :

类编辑器状态:
                                  #改变这里
    定义返回内容(自我,内容):
        返回内容

类编辑器():
    content = '' #这里改变
    def __init__(self):
        自我内容 = ""

    def __str__(self):
        返回 f'{self.content}'

    def setContent(自我,价值):
        自我内容 = 价值

    定义创建内容(自我):
        return EditorState.returnContent(self,self.content) #这里改

    def 恢复(自我,新值):
        self.content = new_value

    定义获取内容(自我):
        返回 self.content

班级历史:
    history = [] #这里改变
    def __init__(self):
        self.history = []

    def __repr__(self):
        返回self.history

    def 推送(自我,价值):
        self.history.append(值)

    def 删除(自我):
        my_list = self.history
        my_list.pop()

        last_index = my_list[-1]
        返回最后一个索引

    def getvalue(自我):
        my_list = self.history
        返回我的列表

编辑器 = 编辑器()
历史=历史()

编辑器.setContent("a")
history.push(editor.createContent())


编辑器.setContent("b")
history.push(editor.createContent())

编辑器.setContent("c")
history.push(editor.createContent())

编辑器.setContent("D")
history.push(editor.createContent())

editor.restore(history.remove())

print(history.history) #更改这里
打印(editor.getcontent())

输出:

在此处输入图像描述

这个函数(在下面的第二张图片中)返回了一个类的对象而不是变量,因为init () 函数只返回空/无数据类型(这是一个规则,箭头标记显示正在返回的数据类型),所以它基本上返回被推送到您的列表中的对象

在这里你可以看到init () 什么都不返回。

在此处输入图像描述

在这里你可以看到什么数据类型被推送到列表中

在此处输入图像描述

还可以尝试在类中全局创建变量,以便在需要时在任何地方轻松访问它们。

于 2020-12-27T15:12:42.600 回答