让它像这样
public Report(){
// declare, create and initialise all in one statement
this.departments = {"Accounting", "Sales", "HR", +
"Administration"} ;
this.grossTotals = {0.0, 0.0, 0.0, 0.0} ;
this.taxTotals = {0.0, 0.0, 0.0, 0.0} ;
} // END constru
实际上,您正在创建构造函数本地的新数组对象,这些对象在构造函数中进行初始化。
您的类字段将使用上面的代码进行初始化。
如果您没有错误地完成此操作,请参阅此文档,它会更好地阐明您的理解 http://download.oracle.com/javase/tutorial/java/nutsandbolts/variables.html
**
Update
:**
上面的代码会给你非法的表达式开始
这是工作代码
class Report
{
// declare instance variables (arrays)
public String[] departments = null;
public double[] grossTotals = null;
public double[] taxTotals = null;
// constructor
public Report(){
this.departments = new String[]{"Accounting", "Sales", "HR", "Administration"} ;
this.grossTotals = new double[]{0.0, 0.0, 0.0, 0.0} ;
this.taxTotals = new double[]{0.0, 0.0, 0.0, 0.0} ;
} // END constructor
}