1

is there a way to update a class I am currently in with a new instance of that class? For example:

public class Test
{
  Test temp = new Test();
  this = temp;
}

I am doing this because I have a save/load feature form serializable and I need a way to update the current class from it

4

1 回答 1

1

不,Java 变量存储对对象的引用,因此this = ...不起作用。Java 无法跟踪引用当前实例的每个位置Temp并更改它们指向的内存引用。

下一个最好的事情可能是将所有字段从一个对象复制到另一个对象。不过,这有其自身的一系列问题

于 2012-11-09T00:36:15.357 回答