【问题标题】:Calling an instance method from a different class从不同的类调用实例方法
【发布时间】:2016-12-06 23:14:55
【问题描述】:

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

它叫做ComplexNumber。这个类包含从加法、乘法、除法复数和其他东西的所有内容,它们都在方法中。老师希望我们创建一个测试类,这是我目前所拥有的。

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

在测试课上,我很难调用这些方法。
我知道我需要类名。

我试过了:

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 类的方法示例:

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;
}

【问题讨论】:

  • 我有两个对象:double real 和 double imag,我不想发布我的 ComplexNumber 类
  • 我的老师说我必须在新班级而不是 ComplexNumber 班级中创建测试程序。
  • 我想要的是如何在另一个类中调用实例方法。
  • 我从 ComplexNumber 类中放入了 plus 方法
  • 你能告诉我我必须做什么,我不明白

标签: java class object methods complex-numbers


【解决方案1】:
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());

    }

}

【讨论】:

  • 这正是我所拥有的,你不明白。我必须告诉计算机要进入哪个类,通过键入类名、方法和参数来找到您要执行的方法:ComplexNumber.round(5.48)
  • 是否要调用这样的方法:ComplexNumber.first.plus(second)?这个有很多不正确的地方
  • 我该如何解决?
  • 首先,运行我在答案中提供的代码,并告诉我您是否得到了正确的结果。好的?然后发布您的整个 ComplexNumber 课程。我怀疑您类中的round() 方法是静态方法。不要让事情变得比现在复杂。删除静态声明
  • 找到问题了,你是对的。它不起作用的原因是因为我输入了 TestComplexNumber,它应该是 ComplexNumber。
【解决方案2】:

我不知道这个“交互面板”是什么,但那里的first.plus(second) 应该与实际代码中的工作没有什么不同。

plus是firstinstance中调用的方法。

方法不应该是static,如static ComplexNumber plus(ComplexNumber other),所以不需要类名即可使用。

总之,要从另一个类调用实例方法,你需要一个实例,你应该有四个对new ComplexNumber()的调用,不 new TestComplexNumber()


我的老师说我必须在新班级而不是 ComplexNumber 班级中创建测试程序

我认为你真正的问题是你有这个类TestComplexNumber,它只是一个“测试类”(只需要一个main 方法),而不是ComplexNumber 类的重新创建,这似乎是你所做的(因为我没有看到加号、乘法、除法等)。


如果您应该实际创建一个“测试套件”,而不是 main 方法,那么您应该使用 JUnit 或其他测试框架

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-01
    • 1970-01-01
    • 2016-08-30
    • 2018-05-14
    • 2010-10-26
    相关资源
    最近更新 更多