我正在尝试继承一个包含方法equals的接口,该方法接收另一个对象,但在类中,我尝试使用类类型,例如: class Grade,并使用Grade覆盖该方法。如果我错了,请纠正我,任何类都继承自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;
    }发布于 2020-11-18 19:06:33
使用泛型:
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());
    }发布于 2020-11-18 19:17:54
忘记了泛型编程,
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;
}
}https://stackoverflow.com/questions/64891809
复制相似问题