简体   繁体   English

我无法理解对象引用在 Java 中是如何工作的?

[英]I'm having trouble understanding how object references work in Java?

The way I understood references originally was that they were simply memory references that held the memory location of the actual object they hold.我最初理解引用的方式是它们只是内存引用,它们保存了它们所持有的实际对象的内存位置。 The code below and its output confuses that for me, though.不过,下面的代码及其输出让我感到困惑。 Here you can see the implementation of a simple class Man.在这里你可以看到一个简单的 Man 类的实现。

I create a Man object in the first line with the reference being called peter.我在第一行创建了一个 Man 对象,引用名为 peter。 peter on its own is just a memory location, right?彼得本身只是一个记忆位置,对吗? So person should just be storing the object in the memory location it is at.所以人应该只是将对象存储在它所在的内存位置。

But when I assign another Man reference to peter and later change peter's name, person does not know this and prints the first name.但是当我将另一个 Man 引用分配给 peter 并稍后更改 peter 的名字时,人不知道这一点并打印了第一个名字。 How can this be since it stores the memory reference for peter?这怎么可能,因为它存储了彼得的内存引用? Shouldn't it be able to follow changes made to it?它不应该能够跟随对其所做的更改吗?

public class Testing {

  public static void main(String[] args) {
    Man peter = new Man("brown", 182, 78000, "Peter");
    Man person = peter;
    peter = new Man("brown", 182, 78000, "Leonard");
    System.out.println(person.name);
  }
}

class Man {

   String hairColor;
   int height;
   double salary;
   String name;

   public Man()
   {
     hairColor = "brown";
     height = 180;
     salary = 50500.5;
     name = "John";
   }
   public Man(String hair, int high, double pay, String nam)
   {
        this.height = high;
        this.hairColor = hair;
        this.salary = pay;
        this.name = nam;
   }
}

Here:这里:

Man peter = new Man("brown", 182, 78000, "Peter");

creates a Man object named "Peter".创建一个名为“Peter”的 Man对象

Man person = peter;

creates another variable "pointing" to the object created above.创建另一个变量“指向”上面创建的对象。

peter = new Man("brown", 182, 78000, "Leonard");

creates another Man named Leonard, and afterwards the peter variable "points" to that new, second object.创建另一个名为 Leonard 的 Man,然后peter变量“指向”那个新的第二个对象。

Note: person didn't "point" to peter .注意: person没有“指向” peter It points to the Man "object" in memory.它指向内存中的 Man “对象”

And putting another "memory address" into the peter variable doesn't change the initial object you created.另一个“内存地址”放入peter变量不会更改您创建的初始对象。

Think of the references as memory addresses.将引用视为内存地址。 I hope this example explains:我希望这个例子能解释:

Man peter = new Man("brown", 182, 78000, "Peter");
// Create a new Man object which is placed in (for example) memory location 100
// Assign 100 to peter


Man person = peter;
// Assign 100 to person (copying it from peter)

peter = new Man("brown", 182, 78000, "Leonard");
// Create a new Man object which is placed in memory location 120
// 120 is assigned to peter

System.out.println(person.name);
// person still contains 100, so this prints out the details of the first object

it works like this:它是这样工作的:

Man peter = new Man("brown", 182, 78000, "Peter");

means you got new place in memory that has those values.意味着您在内存中获得了具有这些值的新位置。

Man person = peter;

means person has same Pointer to the memory of peter.表示人有相同的指向彼得记忆的指针。

peter = new Man("brown", 182, 78000, "Leonard");

you changed the pointer area to new place with new values.您将指针区域更改为具有新值的新位置。

Man peter = new Man("brown", 182, 78000, "Peter");
Man person = peter;
peter = new Man("brown", 182, 78000, "Leonard");

First line assigns a reference to the object with name "Peter", second line assigns previous object reference to variable person .第一行将引用分配给名为“Peter”的对象,第二行将先前的对象引用分配给变量person

In the third line you create a new object and assign a reference to it to variable peter , which is completely new reference, while the variable person still keeps the reference of the previous object.第三行中,您创建了一个新对象并将对它的引用分配给变量peter ,这是全新的引用,而变量person仍然保留前一个对象的引用。

If you wanted to have name "Leonard" in both variables, instead of creating a new object, you could just have in the third line如果你想在两个变量中都有名字“Leonard”,而不是创建一个新对象,你可以在第三行

person.name = "Leonard";

When you use当你使用

= new Man

You create a new object.您创建一个新对象。 So peter is watching Man 1, person is watching a new Man 2所以彼得在看人 1,人在看一个新的人 2

您将两个对象都指向相同的内存/位置,然后将另一个引用分配给第一个对象,这样它就不会影响旧的内存数据。

With peter=new Man() creates the new memory location.Now 2 memory location has been created , lets say 100 and 200. Still person is pointing to peter memory location 100. so that it is displaying first constructor name peter.使用 peter=new Man() 创建新的内存位置。现在已经创建了 2 个内存位置,比如说 100 和 200。仍然有人指向 peter 内存位置 100。因此它显示第一个构造函数名称 peter。

if you wants to display the nam value as Leonardm, add below line assignment如果要将 nam 值显示为 Leonardm,请添加以下行分配

person = Peter;人=彼得;

System.out.println(person.name); System.out.println(person.name);

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

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