【发布时间】:2016-04-14 05:10:01
【问题描述】:
我在将状态抽象方法中的一些变量保存到备忘录模式中的文件时遇到问题。错误是“范围内不可访问”。
以下是代码片段:
状态类。
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);
}
}
我知道它不应该起作用,但是如何解决呢?
【问题讨论】:
-
Save方法中的状态从何而来?
-
Memento 类在 State 类之外的另一个包中,所以我不得不将 State 类从包状态导入到 Memento 类中。