这是一个非常简单的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.root
(Object other
是equals的参数,那我该怎么做?
注意,我的课是public class MyBinaryTree extends Bina
按照Elliots的建议或创建一个get函数来查找所需的确切值。也许做一个检查节点是否为根的函数。您需要检查指向您节点的指针是否指向与root相同的事物。
受保护的变量与此无关。您只需将other
强制转换为BinaryTree
,例如BinaryTree o = (BinaryTree) other; return this.equalsUtil(this.root, o.root);