java - 下面的作业有什么作用?

标签 java

我在《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/

相关文章:

java - 静态 java 字符串周围的奇怪引号字符(在 android 上?)

java - Vaadin 7 和 org.vaadin.addon.customfield。包裹

java - 从 POJO 生成 JSON 样本

java - 可重入锁定

java - 在 Java/Slick2D 中向鼠标位置射击子弹

java - 带有泛型的返回类型

java - 无法使用 Scanner 类

java - mina writefuture 返回 written=true 尽管失败

java - 我无法让我的插件在 MySQL 数据库中存储值

java - 打印原始数组未排序的数组