我有一個類調用另一個類的方法。我在Ubuntu 13.04上編譯並運行它,運行良好。在OSX 10.8.4,我得到下面的輸出:螺紋OSX但不是Ubuntu的NoSuchMethodError
異常 「主要」 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;
}
}
ComplexTester
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");
}
}
你可以發佈代碼? – 2013-08-27 00:21:32
我剛剛發佈了這兩個類。 –
它會給你什麼錯誤?在你的部分堆棧跟蹤中,它給了我們一個行號,但是該行似乎是一條評論。 – mattbdean