我在将状态抽象方法中的一些变量保存到备忘录模式中的文件时遇到问题。错误是“范围内不可访问”。
以下是代码片段:
州级。
public abstract class State
{
protected int W;
public int getW()
{
return W;
}
public void setW(int w)
{
W = w;
}
}
纪念品课。
public class Memento {
private int w, h;
private double health;
private FileWriterUtil fileWriter = new FileWriterUtil("data.txt");
private FileWriterCaretaker caretaker = new FileWriterCaretaker();
public void Save() {
//here is the error in two lines under.
w = state.State.this.getW();
h = state.State.this.getH();
String strI = Integer.toString(w);
String strII = Integer.toString(h);
String str = strI+strII;
fileWriter.write(str);
caretaker.save(fileWriter);
}
}
我知道它不应该工作,但如何解决它?