我有一个从另一个类调用方法的类。我在 Ubuntu 13.04 上编译并运行它,它运行良好。在 OSX 10.8.4 上,我得到以下输出:
线程“主”java.lang.NoSuchMethodError 中的异常:Complex.equals(LComplex;)Z 在 ComplexTester.main(ComplexTester.java:11)
我尝试在 Eclipse、Netbeans 和终端中运行它,我得到了相同的输出。任何帮助将不胜感激。
复杂的
public class Complex {
private double real;
private double imaginary;
/**
*
* @param real the real part of the Complex number
* @param imaginary the imaginary part of the Complex number
*/
public Complex(double newReal,double newImaginary){
real=newReal;
imaginary=newImaginary;
}
/**
*
* @return the real part of the Complex number
*/
public double getReal(){
return real;
}
/**
*
* @return the imaginary part of the complex number
*/
public double getImaginary(){
return imaginary;
}
/**
* gives real a new value
* @param newReal the new value of real
*/
public void setReal(double newReal){
real=newReal;
}
/**
* gives imaginary a new value
* @param newImaginary the new value of Imaginary
*/
public void setImaginary(double newImaginary){
imaginary=newImaginary;
}
/**
*
* @param x the new Complex object whose instance variables must be added to the old one
* @return a new Complex object that is a combination of the parameters of both Complex objects
*/
public Complex add(Complex x){
Complex result=new Complex(x.getReal()+real,x.getImaginary()+imaginary);
return result;
}
/**
*
* @param other the Complex object being compared
* @return true if both Complex objects have the same imaginary and real parts
*/
public boolean equals(Complex other){
return other.getImaginary()==imaginary && other.getReal()==real;
}
}
复杂测试仪
public class ComplexTester {
public static void main(String[] args){
Complex x=new Complex(23.2,33.1);
Complex y=new Complex(23.2,33.1);
//test the equals method
System.out.println(x.equals(y));
System.out.println("Expected: True");
//test the setImaginary() and setReal() methods
x.setImaginary(2);
x.setReal(5);
//test the getImaginary() and getReal() methods
System.out.println(x.getImaginary());
System.out.println("Expected: 2");
System.out.println(x.getReal());
System.out.println("Expected: 5");
//test the equals method again
System.out.println(x.equals(y));
System.out.println("Expected: False");
//test the add method
Complex added=x.add(y);
System.out.println(added.getReal());
System.out.println("Expected: 28.2");
System.out.println(added.getImaginary());
System.out.println("Expected: 35.1");
}
}