询问有关将运行时参数数组传递给新类的问题

时间:2013-04-09 22:21:19

标签: java arrays class object runtime

我试图将main方法中给出的运行时参数数组传递给另一个名为GPA的类。我创建了这个对象,但我不确定我是如何发送它的。我会使用“this”关键字吗?

 class TestGPA
    {

    public static void main(String[] args)
    {
        GPA gpa = new GPA;

        if (args.length == 0)
        {
            System.out.println("Please supply grades to find GPA");
            System.exit(0);
        }
        else
        {
            String[] courseIds = new String[args.length];
            char[] grades = new char[args.length];
            parseInput(args, courseIds, grades);
            displayResult(courseIds, grades, computeGPA(grades));
        }
    }

    }

1 个答案:

答案 0 :(得分:0)

您未正确实例化GPA。它应该是:

GPA gpa = new GPA();

GPA gpa = new GPA;

您的GPA类可能具有属性的getter / setter。例如:

//sorry for the indent, didn't write in IDE

class GPA{
 private String[] whatever;

public void setWhatever(String[] w){
  this.whatever = w;
}

public String[] getWhatever(){

return this.whatever;
}

}

然后在您的main方法中,您可以

GPA gpa = new GPA();
gpa.setWhatever(args);

使用whatever属性:

gpa.getWhatever()