从不同的 class 调用实例方法

Calling an instance method from a different class

这个程序的目的是测试我创建的另一个程序。

叫做ComplexNumber。 class 包含加法、乘法、复数除法等所有内容,它们都在方法中。老师要我们创建一个测试class,这是我目前所拥有的。

我遇到的问题是从 ComplexNumber class 调用方法。例如:我尝试调用 plus 方法,此方法接受两个 ComplexNumber 并将它们相加。到目前为止,我一直在使用交互面板测试这些方法,并且效果很好。我在交互面板中调用它们的方式是 first.plus(Second),这将给出最终值。

在测试中class,我在调用方法时遇到困难。
我知道我需要 class 名称。

我试过:

ComplexNumber.first.plus(second)

但是没有用。
我该怎么做?

这是我的代码:

class TestComplexNumber
{
    double real;         
    double imag;

    public TestComplexNumber(double a, double b)
    {
        this.real=a;
        if ((b<1000)&&(b>-1000))
            this.imag=b;
        else
        {
            this.imag=0;
            System.out.println("The value you typed in for imag is <1000 or >-1000, value of imag is assigned the value of 0.");
        }
    }

    public String toString()
    {
        double real,imag;
        real=this.real;
        imag=this.imag;

        if (((real<0)||(real>0))&&(imag%1!=0))
        {
            if (roundThreeDecimals(imag)>0)
                return ""+roundThreeDecimals(real)+"+"+roundThreeDecimals(imag)+"i";
            else
                return ""+roundThreeDecimals(real)+""+roundThreeDecimals(imag)+"i";
        }
        else if ((real%1!=0)&&(imag!=0))
            return ""+roundThreeDecimals(real)+"+"+(int)imag+"i";
        else if((real==0)&&(imag%1!=0))
            return ""+imag+"i";        
        else if ((real==0)&&(imag !=0))
            return ""+(int)imag+"i";
        else if ((imag==0)&&(real!=0))
             return ""+(int)real+"";
        else if (((real<0)||(real>0))&&(imag<0))
            return ""+(int)real+"-"+(int)Math.abs(imag)+"i";
        else if((real!=0)&&(imag!=0))
            return ""+(int)real+"+"+(int)imag+"i";
        else 
            return "";
     }

     public static double roundThreeDecimals(double c)
     {
         double temp = c*1000;
         temp = Math.round(temp);
         temp = temp /1000;
         return temp;
     }  

    public static void main(String args[])
    {
        for(int i=0;i<1;i++)
        {
            //Testing decimal values
        TestComplexNumber first=new TestComplexNumber((int)(Math.random()*100)-(int)(Math.random()*100),(Math.random()*100));
            TestComplexNumber second=new      TestComplexNumber((Math.random()*100),(Math.random()*100)-(int)(Math.random()*100));
            //Testing whole values
            TestComplexNumber third=new TestComplexNumber((int)(Math.random()*100)-(int)(Math.random()*100),(int)(Math.random()*100));
            TestComplexNumber fourth=new TestComplexNumber((Math.random()*100)-(int)(Math.random()*100),(int)(Math.random()*100));

            System.out.println(first);
            System.out.println(second);
            System.out.println(third);
            System.out.println(fourth);      
            System.out.println("Test value for plus:"+first+second+" which added="+plus(second));  
        }

    }
}

ComplexNumber 上的方法示例 class:

public ComplexNumber plus(ComplexNumber other) {
    ComplexNumber sum= new ComplexNumber(this.real,this.getImag());

    sum.real=(this.real)+(other.real);
    sum.setImag((this.getImag())+(other.getImag()));

    return sum;
}
public class Tester {
    public static void main(String [] args) {
        // create two objects of ComplexNumbers with whatever values you like
        ComplexNumber numA = new ComplexNumber(....);
        ComplexNumber numB = new ComplexNumber(....);

        // then add them and store the returned reference into a new variable
        ComplexNumber result = numA.plus(numB);
        // print the number however you like
        System.out.println(result.real + " + i" + result.getImag());

    }

}

我不知道这个 "interaction panel" 是什么,但是那里的 first.plus(second) 应该与实际代码没有什么不同。

plus是在first实例中调用的方法。

该方法不应是 static,如 static ComplexNumber plus(ComplexNumber other),因此您不需要 class 名称即可使用它。

总而言之,要从另一个 class 调用实例方法,您需要一个实例,您应该有四个调用 new ComplexNumber(),并且 没有 new TestComplexNumber()


My teacher said i have to create the testing program in a new class and not on the ComplexNumber class

我认为你真正的问题是你有这个classTestComplexNumber,它只是一个"test class"(只需要一个main方法),不是 ComplexNumber class 的重新创建,这似乎是您所做的(因为我看不到加号、乘号、除号等)。


如果您实际上应该创建一个 "test suite",而不是 main 方法,那么您应该使用 JUnit 或其他测试框架