我试图使对象的状态传递到“外部”,因此稍后可以通过将它传回来恢复,但不会向外部世界公开私有状态。在Java中,我可以用一个内部类做到这一点:访问内部类的私有字段
class Walker {
class State {
private int pos;
private State (int p) {
pos = p;
}
}
private int pos = 0;
State setState() {
return new State(pos);
}
void setState (State s) {
pos = s.pos;
}
}
不过,如果我尝试在斯卡拉一样的,它说,沃克#的setState可能无法访问状态#POS。
class Walker {
private var pos = 0
def state = new Walker.State(pos)
def state_= (s: Walker.State) {
pos = s.pos
}
}
object Walker {
class State (private val pos: Int)
}
如何存档与Java中相同的东西? (其它的是克隆的对象)
谢谢!我以前不知道那个私有[Something]语法。我甚至没有考虑过这个问题,如果“Walker”可以交换“State”。 – edave 2014-09-28 22:08:08