java - 实现接口的类不能使用自己在 Equals 中插入 Object
问题描述
我试图继承一个接口,该接口包含接收另一个对象的equals方法,但在类中,我试图使用类类型,例如:class Grade,并用Grade other覆盖该方法。如果我错了,请纠正我,任何类都继承自 java 中的 Object 类。我可能不太了解接口。谢谢!
public interface Comparable {
int Bigger(String ... args);
boolean Equals(Object other);
}
@Override
public boolean Equals(Grade other) {
if(other.getGrade() == this.getGrade() && other.getPoints() == this.getPoints() && other.getSubject() == this.getSubject())
return true;
return false;
}
解决方案
使用泛型:
interface Comparable<T> {
// …
boolean Equals(T other);
}
class Grade implements Comparable<Grade> {
// …
@Override
public boolean Equals(Grade other) {
return other.getGrade() == getGrade()
&& other.getPoints() == getPoints()
&& other.getSubject() == getSubject());
}
推荐阅读
- php - 使用 NGINX 处理 Laravel REST API 每秒至少 200 个并发请求
- python - 通过 CSV 文件查找列的模式值
- python - 使用 Python 使用分页抓取谷歌搜索
- laravel - 如何通过单个语句将嵌套关系提取到数组中
- javascript - 如何通过在 html 表中动态插入新行来更改按钮的位置?
- php - opcache_reset 应该在 Windows 上工作吗?
- linux - 当我使用 ipv6 主机名时,ssh 连接在 1m 后被远程关闭
- python - 在python中根据多个条件创建列
- c++ - 我收到警告:控制到达非无效函数的结尾
- node.js - 如何获取网站的API和数据响应