0

我有一个从另一个类调用方法的类。我在 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");



    }
}
4

2 回答 2

2

问题在于您构建和/或运行代码的方式。在 OSX 上,您以某种方式使用Complex.class与源代码不匹配的旧文件运行,并且与ComplexTest.class文件期望的不匹配。

  • 这可能是因为您没有正确重建。

  • 这可能是因为您Complex.class的运行时类路径上有一个旧副本。


FWIW:

  1. “Complex.equals(LComplex;)Z”字符串表示 JVM 期望在Complex类中找到具有此签名的方法:

     boolean equals(Complex)
    

    此方法存在于Complex源代码中...

  2. 如果您打算重载该Object.equals(Object)方法,则您的equals方法不正确。(虽然这是一个不同的问题......)

于 2013-08-27T00:57:53.477 回答
0

尝试改变 x.setImaginary(2); x.setReal(5); 到 x.setImaginary(2.0); x.setReal(5.0);。也许mac上的java版本需要一个double而不是int。

于 2013-08-27T00:45:21.453 回答