【发布时间】:2021-01-30 15:11:33
【问题描述】:
我有一个自定义的 Rectangle 类:
public class Rectangle () {
private int height, width, x, y;
private Color color;
public Rectangle () {
this.height = null;
this.width = null;
this.x = null;
this.y = null;
this.color = null;
}
public void setHeight(int h) { this.height = h; }
public void setWidth(int w) { this.width = w; }
public void setX(int x) { this.x = x; }
public void setY(int y) { this.y = y; }
public void setColor(Color c) { this.color = c; }
public int getWidth() { return this.width; }
public int getHeight() { return this.height; }
public int getX() { return this.x; }
public int getY() { return this.y; }
public Color getColor() { return this.color; }
public void undo() { }
public void redo() { }
}
我将如何为这个类实现撤消和重做功能,使其能够将矩形恢复到以前的状态,而无需用户提及上次使用的方法。我有一个模糊的想法,涉及使用堆栈,但我被困在如何实际编码它上。我的第二个问题是我不确定我的构造函数是否正确,我将所有内容初始化为 null 而不提供任何参数,因为我希望人们改用 getter/setter。请帮忙。
【问题讨论】:
-
好吧,至于你的第一个问题 - 是的,这涉及某种堆栈,可能其中两个(第二个用于重做)。至少在概念上,因为您需要后进先出机制。
-
您的第二个问题无法回答——“正确”取决于要求,我们不知道您的要求。
标签: java