简体   繁体   中英

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

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.

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:

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.

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. This way the reference is never set to null, but is there a better way to do this? I'm adamant I shouldn't be using a List if I only need one value. :(

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).

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.

Assigning a new value to the variable frog afterwards has no effect on the code executed inside returnFrog() .

This becomes more clear when you think of frog as an alias for the instance:

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; 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. 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 .

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM