0

我试图将 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));
        }
    }

    }
4

1 回答 1

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()
于 2013-04-09T22:35:03.930 回答