Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有这两个对象(u1和u2,分别分配给Object 1和Object 2)
u1
u2
Object 1
Object 2
u1 = Object 1; u2 = Object 2;
现在我想稍后切换分配,所以我会使用这个:
u1 = Object 2; u2 = null;
我试过这个:
u1 = u2; u2 = null
但这不起作用,因为u1和u2都被分配了null。
null
我该如何解决这个问题?我不能使用克隆方法,因为我需要的不仅仅是浅拷贝
尝试 :
u1 = Object 1; u2 = Object 2; Object tmp = u1; u1 = u2; u2 = tmp;
为什么你认为这行不通?
Object u1 = new SomeObject(); Object u2 = new SomeObject(); u1 = u2; u2 = null;