我需要帮助编写加法和减法的静态版本。我试过这样做,但我不确定这是否是正确的方法,而且我的 equals 方法也不起作用。当我编译我的 equals 方法时,它总是打印错误。
public static void main(String[] args) {
Complex c1 = new Complex(7, 7);
Complex c2 = new Complex(7, 7);
System.out.println(Complex.add(c1, c2));
System.out.println(c1.equals(c2));
}
}
class Complex {
private static double realPart;
private static double imaginaryPart;
public Complex() {
realPart = 0;
imaginaryPart = 0;
}
public Complex(double c1, double c2) {
realPart = c1;
imaginaryPart = c2;
}
public static Complex add(Complex firstNumber, Complex secondNumber) {
Complex result = new Complex();
firstNumber.realPart = 7;
secondNumber.imaginaryPart = 7;
result.realPart = firstNumber.realPart + secondNumber.imaginaryPart;
return result;
}
public static Complex subtract(Complex firstNumber, Complex secondNumber) {
Complex result = new Complex();
firstNumber.realPart = 7;
secondNumber.imaginaryPart = 7;
result.realPart = firstNumber.realPart + secondNumber.imaginaryPart;
return result;
}
public boolean equals(Complex firstNumber, Complex secondNumber) {
if (firstNumber == secondNumber) {
return true;
} else {
return false;
}
}
public String toString() {
return "result= " + realPart;
}
}