简体   繁体   English

Java,两个对象,object1 = object2 = class / type ...不明白

[英]Java, two objects, object1 = object2 = class/type … don't understand

I have two instance variables, head and tail. 我有两个实例变量,head和tail。 In the code there's a line: 在代码中有一行:

head = tail = new Node<E>();

Does this mean that there are two instances, head and tail, of class Node? 这是否意味着Node类有两个实例,head和tail? I'm quite confused here. 我在这里很困惑。

It simply means: 它只是意味着:

tail = new Node<E>();
head = tail;

So there are 2 references ( head and tail ) pointing to the same Node<E> instance. 因此有2个引用( headtail )指向同一个Node<E>实例。

This means there are TWO references to ONE Object Node . 这意味着有两个对一个对象Node引用。

The line tail = new Node<E>(); line tail = new Node<E>(); actually returns a value (in this case an object reference) equal to the assigned value. 实际上返回一个等于指定值的值(在本例中为对象引用)。

不,只创建了一个Node<E> 实例 ,但headtail引用它,因此你有两个指向同一个对象的引用变量。

Only one instance of Node . 只有一个Node实例。 Both head and tail references pointing to same instance. headtail引用都指向同一个实例。

No certainly not. 当然不是。

Here's what's happening in this code, in sequence. 以下是此代码中按顺序发生的情况。

  1. 'new' is used to create an instance, aka an object, of the Node class 'new'用于创建Node类的实例,也称为对象
  2. a reference to this instance is stored in the tail reference 对此实例的引用存储在尾部引用中
  3. a reference to this instance is stored in the head reference. 对此实例的引用存储在头参考中。

2个引用headtail都分配给一个Node实例。

只创建了一个对象,head和tail都引用了同一个对象。

object1=object2 ;

Here Object1 one refrence to other means simply object2 is copied all addresses to object1 reference 这里Object1一个参考其他意味着简单地将object2复制到object1引用的所有地址

Simply object2 is copied into object1 只需将object2复制到object1中

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

相关问题 JAVA ::将列表对象1复制到两个对象都具有相同变量的列表对象2 - JAVA :: copy List object1 to list object2 where both the objects have same variables 在集合中添加元素 <Object1> 清单 <Object2> 在Java集合中 - Add an element in set<Object1> of List<Object2> in java collections 将列表收集到地图中<Object1 , Map<Object2, Object3> &gt; 在 Java 8 中 - Collect List into a Map<Object1 , Map<Object2, Object3>> in Java 8 将数组的object1移到新的object(object2)并从Java中的数组中删除对象1 - move object1 of an array to a new object(object2) and remove the object 1 from array in java java- field.set(objec1,object2)仅在object1是字符串时有效 - java- field.set(objec1,object2) works only if the object1 is a string 填充列表 <Object1> 基于另一个列表 <Object2> 使用java 8流 - populate a List<Object1> based on another List<Object2> using java 8 stream 如何测试json object1是json object2的子集 - how to test json object1 is subset of json object2 用字符串和列表迭代映射<Object1>并替换为列表<Object2> - Iterate Map with String and List<Object1> and replace to List<Object2> 我们什么时候说Object1 .equals(Object2)是true? - When do we say Object1 .equals(Object2) is true? 如何从在object1中创建的object2调用object1的方法 - How to call a method of object1 from an object2 created in object1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM