简体   繁体   English

如何将引用从一个对象移动到另一个对象?

[英]How can I move a reference from one object to another?

I'm trying to move a reference to an object from one object to another, thus removing the reference of the object when it is moved and adding the reference to the object it is moved to. 我正在尝试将对象的引用从一个对象移动到另一个对象,从而在移动对象时删除对象的引用,并将对该对象的引用添加到它移动到的对象。

I'll try to set the scene as best I can with a few fake classes: Frog, Owner, Customer :D 我将尝试使用一些假类来尽可能地设置场景:Frog,Owner,Customer:D

So I have a Owner who has a collection of Frogs, these can be loaned out to Customers. 所以我有一个拥有青蛙系列的所有者,这些可以借给顾客。 A Customer can only have a reference to one Frog. 客户只能参考一只青蛙。 So when a Frog is loaned out a reference to that Frog can be created in the Customer class and the Frog can be removed from the owners collection of Frogs. 因此,当青蛙被借出时,可以在Customer类中创建对Frog的引用,并且可以从Frogs的所有者集合中删除Frog。

What I want to know is how I reverse this action. 我想知道的是我如何扭转这一行动。 When I want a Customer to give a Frog back, I can make the instance variable for Customer that references the Frog null, but then I'm not sure how to return this Frog to the Owner without a reference to it, for example: 当我希望客户给青蛙回来时,我可以为引用Frog null的Customer创建实例变量,但是我不知道如何在不引用它的情况下将此Frog返回给所有者,例如:

public void returnFrog()
{
owner.returnFrog(frog);
frog = null;
}

As soon as I make the instance variable null, the reference that has been returned to the owner will also become null. 只要我将实例变量设为null,返回给所有者的引用也将变为null。

The only way I can think of doing this is for a Customer to have a List of one Frog and simply remove a Frog to and from a List. 我能想到这样做的唯一方法是让客户拥有一个Frog的List,只需将一个Frog从List中移除。 This way the reference is never set to null, but is there a better way to do this? 这样,引用永远不会设置为null,但是有更好的方法吗? I'm adamant I shouldn't be using a List if I only need one value. 我坚持认为如果我只需要一个值,我就不应该使用List。 :( :(

Or I could not move the objects around and just keep track of whether or not a Frog is linked to a Customer - If it is, it can't be linked to any other customers (This is my solution atm). 或者我无法移动对象,只是跟踪青蛙是否与客户相关联 - 如果是,则无法链接到任何其他客户(这是我的解决方案atm)。

Hopefully i'm not missing something basic. 希望我不会遗漏一些基本的东西。

The code above works, your expectations are probably wrong: The method returnFrog() adds the reference frog (= the instance to which the variable frog points to) to the list of frogs in the owner instance. 上面的代码有效,你的期望可能是错误的:方法returnFrog()将引用frog (=变量frog指向的实例returnFrog()添加到owner实例中的frog列表中。

Assigning a new value to the variable frog afterwards has no effect on the code executed inside returnFrog() . 之后为变量frog分配一个新值对returnFrog()内部执行的代码没有影响。

This becomes more clear when you think of frog as an alias for the instance: 当您将frog视为实例的别名时,这变得更加清晰:

Frog a = new Frog();
b = a; // doesn't copy the frog
b = null; // doesn't change a

This doesn't copy the frog; 这不会复制青蛙; there is still just a single instance but at different times, you have different variables contain references that point to this instance. 仍然只有一个实例,但在不同的时间,您有不同的变量包含指向此实例的引用。 But changing the reference b doesn't affect a since assigning instances to references doesn't change the instances; 但是更改引用b不会影响a因为为引用分配实例不会更改实例; it just changes the reference. 它只是改变了参考。

I think you've misunderstood how variables and references work. 我认为你误解了变量和引用是如何工作的。

This line: 这一行:

frog = null;

Assigns the value null to the variable frog , which i assume is an instance variable of the Customer class. 将值null赋给变量frog ,我假设它是Customer类的实例变量。 It does not affect any other variable! 它不会影响任何其他变量!

So, if this line: 所以,如果这一行:

owner.returnFrog(frog);

Ends up storing a reference to the amphibian in question into a variable in the Owner , then that reference will still be there after the assignment of null to the variable in User . 最终将对所讨论的两栖动物的引用存储到Owner的变量中,然后在将null赋值给User的变量之后,该引用仍将存在。

A physical metaphor would be writing addresses of buildings on pieces of paper. 一个物理比喻就是在纸上写下建筑物的地址。 Say you have a piece of paper with the address of a good hairdresser on, and i have a blank piece of paper. 假设你有一张纸上有一个好发型师的地址,我有一张白纸。 You tell me the address, and i write it down. 你告诉我地址​​,然后我把它写下来。 Now you erase your piece of paper. 现在你擦掉你的纸。 Does my piece of paper become blank? 我的那张纸变成了空白吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用OpenJPA将对象从一个表“移动”到另一个表? - How can I “move” an object from one table to another using OpenJPA? 如何将对象形状从一个位置移动到另一位置? - How would I move an object shape from one location to another? 如何从另一个JFrame引用一个JFrame中的变量? - How can I reference a variable in one JFrame from another JFrame? 在Java中,我可以引用在另一个类中的一个类中创建的对象吗? - In Java can I reference an object made in one class in another? 我如何将请求对象从一个类传递到另一个类 - how can i pass request object from one class to another 如何将 Bitmap 对象从一个活动传递到另一个活动 - How can I pass a Bitmap object from one activity to another 如何通过坐标使对象从一个点移动到另一个点? - How to make an object move from one point to another via coordinates? 如何将物体从一个点以固定角度均匀地移动到另一点? - How to move an object uniformly from one point to another at a fixed angle? 如何使用Android Canvas将图像从一个点移动到另一个点 - How can I move an image from one point to another using Android Canvas 如何在MouseListener中将一帧移到另一帧 - How can i move one frame to another in MouseListener
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM