我的類有一個數組作爲其成員之一。在構造函數中,我想將它的成員填充到以下內容中。我該怎麼做,它給了我一個錯誤。聲明後填充數組成員
public class GetCurrentMonth {
Calendar now;
String[] monthNames;
/**
*
*/
public GetCurrentMonth() {
now = Calendar.getInstance();
monthNames = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
}
public String getMonth()
{
return monthNames[(now.get(Calendar.MONTH) + 1)];
}
}
我不想在構造函數如下添加這些月份的名字,因爲我想跟進良好的編碼習慣,我被告知,所有的初始化應在構造函數中完成。
String[] monthNames = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};