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