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

问题描述 投票:0回答:2

这是一个非常简单的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

java binary-tree binary-search-tree instance-variables protected
2个回答
0
投票

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


0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.