如何在另一个类中重新分配引用类型并使更改保持原始状态?
这是我所要求的一个相当人为的示例:
public partial class Form1 : Form
{
private Fruit fruit;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
fruit = new Apple();
Geneticist geneticist = new Geneticist(fruit);
// Later on in program..
geneticist.Engineer();
Console.WriteLine(fruit.Color);
// Still red because a copy of fruit was made in Geneticist class.
}
}
class Fruit
{
public string Color { get; set; }
}
class Apple : Fruit
{
public Apple()
{
Color = "Red";
}
}
class Banana : Fruit
{
public Banana()
{
Color = "Yellow";
}
}
class Geneticist
{
private Fruit fruit;
private Banana banana;
public Geneticist(Fruit fruit)
{
this.fruit = fruit;
this.banana = new Banana();
}
public void Engineer()
{
fruit = banana;
}
}
基本上,我在主窗体中存储了一个水果作为成员变量。我希望能够将它传递给我的遗传学家课程,然后让它重新分配值。
当我输入 fruit = Banana;
遗传学家中的水果不再指向 Form1 水果,而是指向遗传学家中的本地副本。我想,我正在寻找一种模拟 ref 关键字的方法,如果我重新分配遗传学家水果,Form1 水果也会随着更改而更新。
我想我可以制作一个水果包装纸并将其传递出去,但这似乎有点黑客。另外,我可以让 Engineer
方法引发一个事件,以便主窗体可以重新分配值,但必须在程序的许多部分执行此操作似乎也有点混乱。
另外,我无法使用 ref 关键字,因为我稍后修改它,而不是在 Geneticist 的构造函数中修改它。
感谢您的阅读!
Here is a rather contrived example of what I am asking:
public partial class Form1 : Form
{
private Fruit fruit;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
fruit = new Apple();
Geneticist geneticist = new Geneticist(fruit);
// Later on in program..
geneticist.Engineer();
Console.WriteLine(fruit.Color);
// Still red because a copy of fruit was made in Geneticist class.
}
}
class Fruit
{
public string Color { get; set; }
}
class Apple : Fruit
{
public Apple()
{
Color = "Red";
}
}
class Banana : Fruit
{
public Banana()
{
Color = "Yellow";
}
}
class Geneticist
{
private Fruit fruit;
private Banana banana;
public Geneticist(Fruit fruit)
{
this.fruit = fruit;
this.banana = new Banana();
}
public void Engineer()
{
fruit = banana;
}
}
Basically, I have a fruit stored as a member variable in my main form. I want to be able to pass it to my Geneticist class and later on have it re-assign the value.
When I type fruit = banana;
the fruit in geneticist no longer points to the the Form1 fruit but instead to the local copy in Geneticist. I am looking for a way to simulate the ref keyword, I suppose, where if I re-assign the geneticist fruit the Form1 fruit is also updated with the change.
I suppose I could create a wrapper of fruit and pass that around instead but that seems a bit hackish. Also I could have the Engineer
method raise an event so that the main form can re-assign the value but having to do that at many parts of my program seems a bit messy as well.
Also, I cannot use the ref
keyword because I modify it later on, not in the constructor of Geneticist.
Thanks for reading!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,这是错误的。不会进行任何复制。
发生的情况是,您用对
Banana
的引用覆盖了您手头的Apple
的引用。就好像你有一个苹果放在你的口袋里,你拿着它一会儿后,你把它放在地上,拿起一根香蕉,然后把它放进你的口袋里。当您稍后决定吃原始苹果时,它会完好无损,但不是因为您复制了。只是因为你对它失去了兴趣,而是得到了一种完全不相关的水果。
那么该怎么办?
事实仍然是,您无法使用引用语义将
Fruit
参数作为一个整体进行修改(CLR 不允许将引用存储为类成员,因为你自己也说过了)。如果您希望Geneticist 修改Fruit 引用,那么您必须围绕它创建一个包装器。但最实用的解决方案是让调用代码与
Geneticist
合作:调用代码:
另一种可行的方法
让
Geneticist
怎么样?知道如何修改水果本身吗?以及调用代码:
No, this is wrong. No copy is made.
What happens is that you overwrite the reference you have at hand of the
Apple
with a reference to aBanana
. It's as if you are given an apple to put in your pocket and after holding on to it for a little while, you leave it on the ground, pick up a banana, and place it in your pocket.When at some later point you decide to eat the original apple will be intact, but not because you made a copy. Only because you just lost interest in it and got hold of a totally unrelated fruit instead.
So what to do?
The fact remains that you cannot modify the
Fruit
parameter as a whole with reference semantics (the CLR does not allow refs to be stored as class members, as you yourself have said).If you want
Geneticist
to modify theFruit
reference, then you do have to create a wrapper around it. But the most practical solution would be to have the calling code cooperate withGeneticist
:And the calling code:
Another workable approach
What about letting the
Geneticist
know how to modify the fruit themselves?And the calling code: