gpt4 book ai didi

java - 如何在 equals 函数中调用子类方法中的 protected 变量?

转载 作者:行者123 更新时间:2023-12-01 21:48:21 25 4
gpt4 key购买 nike

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

最佳答案

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

关于java - 如何在 equals 函数中调用子类方法中的 protected 变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58772419/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com