同一个变量可以有两个名称吗?

发布于 2024-08-25 13:39:08 字数 863 浏览 6 评论 0原文

问题的简短版本:

我做:x = y。然后我更改x,而y没有改变。我想要的是“绑定”xy,这样每当我改变x时我就改变y >。

扩展版本(包含一些细节):

我编写了一个类(“第一个”类),它生成另一个类(“第二个”类)的对象。更详细地说,第二类的每个对象都有一个名称作为唯一标识符。我使用第二个类的对象名称调用第一个类的静态方法。第一个类检查是否已经生成了这样的对象(如果它存在于第一个类的静态 HashMap 中)。如果它已经存在,则将其返回。如果还不存在,则会创建它,将其添加到 HashMap 中并返回。

然后我有以下问题。在程序的某个阶段,我从第一个类的 HashMap 中获取一个具有特定名称的对象。我对这个对象做了一些事情(例如更改某些字段的值)。但是HashMap中的对象却看不到这些变化!所以,事实上,我不会从 HashMap 中“获取”一个对象,而是“创建”这个对象的副本,这是我想避免的。

添加:

正如答案中提到的,我不应该有所描述的行为。而我实际上没有它(我误解了我的程序的行为)。我在对象名称上犯了错误。我想通过名称引用现有对象,但使用了错误的名称,因此我实际上创建了一个新对象,这就是为什么我没有看到对旧对象所做的任何更改。

但无论如何,我了解到,如果我从 HashMap 中获取一个对象并对这个对象进行一些更改,它们也将应用于“坐在”HashMap 中的对象。因此,我可以对同一对象提供不同的引用,并且可以使用这些引用中的任何一个来更改对象的状态。

谢谢您的回答。

The short version of the question:

I do: x = y. Then I change x, and y is unchanged. What I want is to "bind" x and y in such a way that I change y whenever I change x.

The extended version (with some details):

I wrote a class ("first" class) which generates objects of another class ("second" class). In more details, every object of the second class has a name as a unique identifier. I call a static method of the first class with a name of the object from the second class. The first class checks if such an object was already generated (if it is present in the static HashMap of the first class). If it is already there, it is returned. If it is not yet there, it is created, added to the HashMap and returned.

And then I have the following problem. At some stage of my program, I take an object with a specific name from the HashMap of the first class. I do something with this object (for example change values of some fields). But the object in the HashMap does not see these changes! So, in fact, I do not "take" an object from the HashMap, I "create a copy" of this object and this is what I would like to avoid.

ADDED:

As it was mentioned in the answers, I should not have the described behavior. And I actually do not have it (I misinterpreted the behavior of my program). I made an mistake with name of objects. I wanted to refer to an existing object by its name and I used a wrong name, so I actually created a new object and it's why I did not see any changes that I made to the old object.

But anyway, I learned that if I take an object from a HashMap and do some changes to this object, they will be also applied to the object "sitting" in the HashMap. So, I can gave different references to the same object and I can use any of these references to change the state of the object.

Thank you for your answers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梅窗月明清似水 2024-09-01 13:39:08

嗯,你不能给变量起别名。但是,您应该注意,如果您对 xy 引用的对象进行更改,那么这些更改将被通过任一变量可见。引用类型变量的值是引用,而不是对象本身。

听起来在你的情况下应该是这种情况 - 除非你明确地进行了一些克隆,否则每个名称实际上应该只有一个对象,并且你通过一个对象对该对象进行的任何更改变量应该通过其他人或通过地图可见。对象克隆不会自动发生。

我建议您尝试创建一个简短但完整的示例程序来演示该问题 - 我怀疑在这样做的过程中,您会在代码中发现一个错误来解释该行为。

Well, you can't alias variables. However, you should be aware that if you make changes to an object which is referred to by both x and y, then those changes will be visible via either variable. The value of a reference type variable is a reference, not the object itself.

It sounds like this should be the case in your situation - unless you've explicitly put some cloning in place, there should only actually be one object per name, and any changes you make to that object via one variable should be visible via others or via the map. Object cloning doesn't just happen automatically.

I suggest you try to create a short but complete example program which demonstrates the problem - I suspect that in the process of doing so, you'll find a bug in your code which explains the behaviour.

沉默的熊 2024-09-01 13:39:08

我开始写一个很长的答案,但乔恩更快。正如他所说,你想要的行为应该是默认行为,除非你自己做了什么。

I started to write a long answer, but Jon was faster. As he says the behavior you want should be the default one, unless you did something there yourself.

请持续率性 2024-09-01 13:39:08

这是一个有趣的问题,它将带我们了解 = 运算符背后的魔力。

该运算符是一个赋值运算符,它将变量y 的引用赋给变量x。也就是说,如果引用y处的对象发生变化,则引用处的对象也会发生变化。

当您调用 addd 时,这是集合的典型情况。如果 x 和 y 是对同一集合的引用,则在此集合上调用 add 将更新这两个引用。

但是,我认为这就是您所处的情况,而 String 对象则不是这种情况。当您尝试更新字符串时(例如通过调用 +),Java 编译器实际上会创建另一个具有新值的变量并更改引用。因此,分配先前的值不会反映新值的变化。

That's an interesting question, which will brings us in the magics behind the = operator.

This operator is an assignment operator, that will assign to the variable x the reference of the variable y. That's to say if the object at the reference y changes, the obejct at reference will also be changed.

This is typically the case for collections, when you call addd. If x and y are references to the same collection, calling add on this collection will update both references.

But, and I think it's the case you're in, it's not the case for String objects. When you try to update a String, as an example by calling +, the Java compiler in fact creates an other variable with the new value and changes the reference. As a consequence, assigning previous value will not reflect change in new value.

︶ ̄淡然 2024-09-01 13:39:08

在程序的某个阶段,我从第一个类的 HashMap 中获取具有特定名称的对象。我对这个对象做了一些事情(例如更改某些字段的值)。但是HashMap中的对象却看不到这些变化!所以,事实上,我并不是从 HashMap 中“获取”一个对象,而是“创建”这个对象的一个​​副本,这是我想避免的。

使用 get()HashMap 获取对象不是复制操作。因此,除非您自己明确复制它,否则这听起来像是一个线程问题;事实上,从线程 X 更改对象的字段对于线程 Y 可能可见,也可能不可见,即使它与您正在查看的对象完全相同。这是一个相当棘手的领域;我建议获取一份Java 并发实践

At some stage of my program, I take an object with a specific name from the HashMap of the first class. I do something with this object (for example change values of some fields). But the object in the HashMap does not see these changes! So, in fact, I do not "take" an object from the HashMap, I "create a copy" of this object and this is what I would like to avoid.

Taking objects from a HashMap with get() is not a copying operation. So unless you're explicitly copying it yourself, this sounds like a threading issue; indeed, changing the fields of an object from thread X may or may not be visible to thread Y, even if it's exactly the same object that you're looking at. This is quite a tricky field; I recommed getting a copy of Java Concurrency In Practice.

囚你心 2024-09-01 13:39:08

使用 JavaFX:

var y = 10;
def x = bind y;

println(x); // prints 10

y = 12;

println(x); // prints 12

好的,这不是您问题的答案,但我喜欢它的绑定机制 ^^。

Use JavaFX:

var y = 10;
def x = bind y;

println(x); // prints 10

y = 12;

println(x); // prints 12

Okay, not an answer to your problem, but I love it's binding mechanism ^^.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文