Java返回方法使用的对象,而不是基对象
我试图在Java中重写compareTo方法。出于某种原因,此对象(或基础对象)被覆盖,并与“其他”对象或插入到方法中的对象进行比较
有人知道为什么会这样吗?我不认为我在任何地方凌驾于“这个”之上
测试时,我创建了两个多项式:
- 多项式1:42,-7,0,5
多项式2:37,-2,0,0,6
预计为42,-7,0,5
“其他”预计为:37、-2、0、0、6
然而,“这个”最终是42,-7,0,5
代码:
public class Polynomial implements Comparable<Polynomial>
{
private static int[] coefficients;
public Polynomial(int[] coefficients){
if (coefficients.length < 1) {
this.coefficients = new int[]{0};
}
else {
int current_position = coefficients.length -1;
while(coefficients[current_position] == 0 && current_position > 0){
current_position--;
}
this.coefficients = new int[current_position +1];
System.arraycopy(coefficients, 0, this.coefficients, 0, current_position +1);
System.out.println("this" + this);
}
};
@Override public String toString(){
StringBuilder sB = new StringBuilder();
for (int exponents = 0; exponents < coefficients.length; exponents ++){
if (exponents > 0){
sB.append (" + ");
}
sB.append("(");
sB.append(Integer.toString(coefficients[exponents]));
sB.append(")z^");
sB.append(Integer.toString(exponents));
}
return sB.toString();
};
public int getDegree(){
if(coefficients.length <= 1){
return 0;
}
else {
return coefficients.length - 1;
}
};
public int getCoefficient(int k){
if(k >= coefficients.length)
{
return 0;
}
else {
return coefficients[k];
}
};
public static long evaluate(int x){
ArrayList<Integer> toSum = new ArrayList<Integer>();
int summedValue = 0;
toSum.add(coefficients[0]);
for (int i = 1; i <= coefficients.length -1; i++){
int value = x;
for (int y = 0; y < i -1; y++){
value = value * x;
}
value = value * coefficients[i];
toSum.add(value);
}
summedValue = toSum.stream().mapToInt(value -> value).sum();
return summedValue;
};
@Override public boolean equals(Object other){
if (other == this) return true;
if (other == null) return false;
if (other.getClass() != this.getClass()) return false;
Polynomial that = (Polynomial) other;
if (this.getDegree() != that.getDegree()) return false;
for (int i = this.getDegree(); i >= 0; i--)
if (this.coefficients[i] != that.coefficients[i]) return false;
return true;
};
@Override public int hashCode(){
return Arrays.hashCode(coefficients);
};
public int compareTo(Polynomial other){
System.out.println("this" + );
System.out.println("other" + other);
return 0;
System.out.println("this " + this.getDegree());
System.out.println("other " + other.getDegree());
if (this.getDegree() < other.getDegree()) return -1;
else if (this.getDegree() > other.getDegree()) return +1;
else {
for (int i = coefficients.length - 1; i >= 0; i--) {
System.out.println("thisC " + this.coefficients[i]);
System.out.println("otherC " + other.coefficients[i]);
if (this.coefficients[i] < other.coefficients[i]) return -1;
if (this.coefficients[i] > other.coefficients[i]) return +1;
}
}
return 0;
};
public static void main(String[] args) {
int[] c1 = {42, -7, 0, 5};
int[] c2 = {37, -2, 0, 0, 6};
Polynomial p1 = new Polynomial(c1);
System.out.println("p1" + p1);
Polynomial p2 = new Polynomial(c2);
System.out.println("p2" + p2);
System.out.println("compare" + p1.compareTo(p2));
}
}
# 1 楼答案
首先,您应该使
coefficients
不是静态的。其次,您的方法evaluate
也不应该是静态的。 然后您的逻辑在方法compareTo
中是正确的: