[英]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个引用( head
和tail
)指向同一个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>
实例 ,但head
和tail
引用它,因此你有两个指向同一个对象的引用变量。
Only one instance of Node
. 只有一个Node
实例。 Both head
and tail
references pointing to same instance. head
和tail
引用都指向同一个实例。
No certainly not. 当然不是。
Here's what's happening in this code, in sequence. 以下是此代码中按顺序发生的情况。
2个引用head
和tail
都分配给同一个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.