C# 使一个类的一个实例等于另一个–;怎么取消呢?
我有: 我如何将它们彼此断开,以便改变一个不会影响另一个 编辑:我希望它们引用同一个对象(因此我无法克隆),稍后–不。但是我仍然需要这个类的两个实例——所以我不能“null”它们 谢谢 编辑: 编辑: 答案是: @ispiro但是当你说a.integer=1时,你不是在改变指针,你是在跟随指针并改变它末尾的值戴维8 我原以为同时更改“a”和“a.integer”是相同的,因为更改它们要么会更改指针-“a”,要么不会。但事实上:第一个有,第二个没有 谢谢大家 因此,在上面的示例中,如果我添加:C# 使一个类的一个实例等于另一个–;怎么取消呢?,c#,C#,我有: 我如何将它们彼此断开,以便改变一个不会影响另一个 编辑:我希望它们引用同一个对象(因此我无法克隆),稍后–不。但是我仍然需要这个类的两个实例——所以我不能“null”它们 谢谢 编辑: 编辑: 答案是: @ispiro但是当你说a.integer=1时,你不是在改变指针,你是在跟随指针并改变它末尾的值戴维8 我原以为同时更改“a”和“a.integer”是相同的,因为更改它们要么会更改指针-“a”,要么不会。但事实上:第一个有,第二个没有 谢谢大家 因此,在上面的示例中,如果我添加: c
class myclass
{
public int integer;
}
它将将“a”更改为“b”以外的其他点。
但是:
没有。为有问题的类编写一个副本构造函数,并将其中一个引用指向通过副本构造函数创建的新实例。不清楚您想做什么。显然,如果设置了
instance1=null
,则引用将不相同。你是不是在问如何制作深度拷贝
a.integer = 1;
您可能需要查看接口。您必须编写将一个实例的属性复制到另一个实例的代码
或者,您可以重载=
运算符。或者创建一个可以调用的构造函数:
MyClass instance1 = new MyClass();
MyClass instance2 = new MyClass();
instance1 = instance2;
instance1 = null; //does not affect instance2.
假设你在一张纸上写上“宾夕法尼亚大道1600号”。这个地址指的是一所房子,实际上是白宫
假设你在第二张纸上写上“宾夕法尼亚大道1600号”。该地址指的是与第一张纸相同的房子
你绝对无法改变这两个地址指的是同一所房子的事实。你可以把其中一张纸改成“123芝麻街”,然后他们就不再指同一所房子了,但这并没有改变关于白宫的事实,而是改变了关于一张纸的事实 如果你把白宫漆成蓝色,你仍然没有改变任何一张纸;现在两个都是蓝色的房子
你能更详细地解释一下你想在这里做什么吗?你的问题很令人困惑。根据你的评论,我想我能更好地理解你说的话 答案很简单 此代码:
MyClass instance2 = new MyClass(instance1);
不以任何方式链接instance1和instance2,除非它们指向同一事物。您始终可以重新指向任一变量(包括“置零”它们),而不会影响另一个变量。将任一实例设置为null都不会更改对象,它只会使对象停止指向内存中的对象
本质上,这个问题的前提是不准确的。这里没有需要解决的问题
需要理解的关键点:变量instance1和instance2只存储
对象,而不是实际对象。如果要操纵实际对象,可以在该对象上使用方法或属性,例如:
instance1 = instance2;
更新:
假设a和b是带有邮箱地址的索引卡,邮箱可以容纳一个号码。下面是您的代码正在执行的操作
instance1.ChangeColor; // call the ChangeColor method on the object pointed to by instance1.
您需要一种克隆实例的方法(创建实例的副本) 例如,在您的课堂上:
//create a mailbox (call it X) and write the address on index card A
myclass a = new myclass();
//put the number 1 in the mailbox at the address written on index card A (currently x)
a.integer = 1; //mailbox x now contains 1
//create ANOTHER mailbox (call it Y) and write the address on index card B
myclass b = new myclass();
//put the number 2 in the mailbox at the address written on index card B (currently Y)
b.integer = 2; //mailbox y now contains 2
// change the address on index card A to the address from card B (currently Y)
a = b; //both cards now have the address of mailbox Y (which contains 2) written on them.
// if you want a to go back to having the address of mailbox X, you are
// out of luck because you don't have it written down on any of your cards anymore.
//put the number 1 in the mailbox at the address pointed to on Card A (Currently Y)
a.integer = 1; //mailbox Y now contains 1
//Set text to the number from the mailbox at the address on card B (currently Y)
Text = b.integer.ToString();
然后您可以执行以下操作:
public MyClass(MyClass copy)
{
// ...copy all attributes, be careful with reference variables
}
它们都具有完全相同的属性和属性(如果您正确实现了复制构造函数),但修改其中一个不会影响另一个。您的类是否可以改为结构,使其成为值类型
instance1 = new MyClass(instance2);
您的代码:
public void Main()
{
MyObj a = new MyObj{ integer= 1};
MyObj b = a;
Console.WriteLine(a.integer); //1
Console.WriteLine(b.integer);//1
b.integeter = 42;
Console.WriteLine(a.integer);//1
Console.WriteLine(b.integer);//42
}
public struct MyObj
{
public int integer{get; set;}
}
如果您保留一份参考资料:
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
变量是对象的标签,而不是对象本身。在您的例子中,原始的a
不再有对它的引用,因此即使它存在,也无法访问它。(并且,每当垃圾回收器开始处理它时,它就会停止存在,除非有其他引用)
如果有帮助的话,可以这样想,当你说
a=b
或a=new myclass()
时,你正在移动a
所指的线。当你说a.integer=1
时,
有点像说跟随a
所指向的那一行,然后改变目的地。我建议,将类类型的字段和变量看作是保存“实例id”最有帮助。有些人认为它们是指针,但这是一个实现细节。需要实现的基本点是,如果Foo是类类型Bar的变量,则语句“Foo.Boz()”不会指示编译器以任何方式修改Foo;它指示编译器查找存储在Foo中的实例id引用的对象(对象必须是Bar类型),并在该对象上运行Boz方法。很少有操作可以在实例id本身上执行,而不是在引用的对象上执行:
instance1 = new MyClass(instance2);
public void Main()
{
MyObj a = new MyObj{ integer= 1};
MyObj b = a;
Console.WriteLine(a.integer); //1
Console.WriteLine(b.integer);//1
b.integeter = 42;
Console.WriteLine(a.integer);//1
Console.WriteLine(b.integer);//42
}
public struct MyObj
{
public int integer{get; set;}
}
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
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.