实现接口的java类不能在equals中使用自身而不是对象

qni6mghb  于 2021-07-11  发布在  Java
关注(0)|答案(2)|浏览(321)

我尝试继承一个接口,该接口包含接收另一个对象的方法equals,但在类中,我尝试使用类类型,例如:class grade,并用grade other重写该方法。如果我错了,请纠正我,任何类都继承自java中的对象类。我可能不太了解接口。谢谢!

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;
    }
kknvjkwl

kknvjkwl1#

使用泛型:

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());
    }
bbmckpt7

bbmckpt72#

忘记了泛型编程,

public interface Comparable<T>{

int Bigger(String ... args);

boolean Equals(T other);
}

public class Grade implements Comparable<Grade>{
@Override
public boolean Equals(Grade other) {
    if(other.getGrade() == this.getGrade() && other.getPoints() == this.getPoints() && other.getSubject() == this.getSubject())
        return true;
    return false;
}
}

相关问题