制成的类等于另一个的一个实例。 - 如何取消? [英] Made one instance of a class equal to another. – How to cancel that?

查看:138
本文介绍了制成的类等于另一个的一个实例。 - 如何取消?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有:

instance1 = instance2;

如何断开它们彼此,以便改变一个不会影响其他

how do I disconnect them from one another, so that altering one will not affect the other?

编辑:我希望他们引用同一个对象(所以我不能克隆),后来 - 不是。但是,我仍然希望类的两个实例 - 所以我不能'空'他们

I want them referencing the same object (so I can’t clone), and later – not. But I still want both instances of the class – so I can’t ‘null’ them.

感谢

编辑:

myclass a = new myclass();
a.integer = 1;

myclass b = new myclass();
b.integer = 2;

a = b;
//All changes to one will affect the other, Which is what I want.



//<More lines of the program>



//Now I want 'a' to point to something else than 'b'. and I’m missing the code
//so that the next line will not affect 'b'.
a.integer = 1;

Text = b.integer.ToString();
//I need b.integer to still be = 2, it’s not.

使用:

class myclass
{
    public int integer;
}

编辑:

的是的答案:
@ispiro但是当你说a.integer = 1,你并没有改变指针,你是以下的指针,并在它的结束更改值。 - Davy8

This is the answer: @ispiro but when you say a.integer = 1 you aren't changing the pointer, you are following the pointer and changing the value at the end of it. – Davy8

我原以为这两种转变的'a'和'a.integer是在这个意义上改变它们要么改变指针'A'还是会不一样的。但事实上:第一呢,第二个没有

I had thought that changing both ‘a’ and ‘a.integer’ would be the same in the sense that changing them would either change pointer-‘a’ or won’t. But in fact: the first does, the second doesn’t.

谢谢大家。

所以在上面的例子,如果我补充一下:

So in the example above, if I add:

a = c;// where c is another instance of 'myclass'.

它的更改'A'指向其它地方比'B'。
但是:

It will change ‘a’ to point somewhere else than ‘b’. But:

a.integer = 1;

没有。

推荐答案

您code:

myclass a = new myclass();
a.integer = 1;

myclass b = new myclass();
b.integer = 2;

a = b;

//here I need code to disconnect them

a.integer = 1;

Text = b.integer.ToString();
//I need b.integer to still be = 2

如果您保持周围的引用:

If you keep around a reference:

myclass a = new myclass();
a.integer = 1;

myclass b = new myclass();
b.integer = 2;

var c = a; //Keep the old a around
a = b;

//here I need code to disconnect them
a = c; //Restore it.

a.integer = 1;

Text = b.integer.ToString();
//It's still 2 now.

变量是标签的对象,而不是对象本身。你的情况,原始 A 不再需要它的引用,以便即使它存在,就没有办法访问它。 (它会停止,除非有其他的引用它存在每当垃圾收集得到各地摆脱它)

Variables are labels to the objects, not the objects themselves. In your case, the original a no longer has a reference to it so even though it exists, there's no way to access it. (and it'll cease to exist whenever the garbage collector gets around to getting rid of it unless there are other references to it)

如果有帮助,认为它这样,当你说 A = B A =新MyClass的()你是移动,其中 A 指向行。当你说 a.integer = 1 是一种像说按照行指向,然后更改目的地。

If it helps, think of it this way, when you say a = b or a = new myclass() you are moving the line where a is pointing. When you say a.integer = 1, the . is kind of like saying follow the line a is pointing to, then change the destination.

这篇关于制成的类等于另一个的一个实例。 - 如何取消?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆