我在《Java Structures》一书中找到了这个 BinaryTree 类实现,但我不完全理解构造函数的最后一个赋值的作用(一个语句中的三个赋值)。
protected E val; // value associated with node
protected BinaryTree<E> parent; // parent of node
protected BinaryTree<E> left, right; // children of node
public BinaryTree()
// post: constructor that generates an empty node
{
val = null;
parent = null;
left = right = this;
}
最佳答案
您可以将此作业视为两个实际作业:
right = this;
left = right;
因为赋值将从右到左进行评估,因此为了更好地理解,left = right = this
等于left = (right = this)
。因此,right = this
赋值的结果基本上将引用 right
指向的内容,然后将其分配给 left
字段。
关于java - 下面的作业有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56924621/