我正在研究多项式计算器。我的问题是equals方法。以下是相关代码:
public class Poly{
Term[] terms;
//Constructors-------------------------------------------
public Poly() {}
public Poly(ArrayList<Term> Terms) {
terms = Terms.toArray(new Term[Terms.size()]);
Arrays.sort(terms, new TermComparator());
}
//Methods-------------------------------------------------
public boolean equals(Poly x) {
boolean q=false;
if(this == x){
q=true;
}
return q;
}
//used in constructor to order terms
class TermComparator implements Comparator<Term> {
@Override
public int compare(Term t1, Term t2) {
return t2.getExp() - t1.getExp();
}
}
}
即使两个 Poly 对象具有相同的值,equals 方法也始终返回 false。有人可以帮忙吗?