如何在equals函数的子类方法中调用受保护的变量?

这是一个非常简单的equals方法。

这就是我的代码中的内容:

public boolean equals(Object other) 
      if (other == null) {
                return false;
            }
            if (!(other instanceof BinaryTree)) {
                return false;
            }
            return this.equalsUtil(this.root,other.root);
        }

这是第一个问题

public class BinaryTree {
    protected class Node {
        //instancee variables and a constructor
    }
    protected Node root;
    //remainder ommited for brevity

我无法调用other.rootObject other是equals的参数),那我该怎么做?

请注意,我的课程是public class MyBinaryTree extends Bina

wangyaping570133559 回答:如何在equals函数的子类方法中调用受保护的变量?

按照Elliots的建议或创建一个get函数来查找所需的确切值。也许做一个检查节点是否为根的函数。您需要检查指向您节点的指针是否指向与root相同的事物。

,

受保护的变量与此无关。您只需要将other强制转换为BinaryTree,例如BinaryTree o = (BinaryTree) other; return this.equalsUtil(this.root,o.root);

本文链接:https://www.f2er.com/3135645.html

大家都在问