1 回答

TA貢獻1854條經驗 獲得超8個贊
請更正并完成問題中的類,將其復制到編輯器中,并確保使用預覽時它們看起來正確。我不明白B 類中的testinprint(test.arr[0]);來自哪里。但是,您正在打印尚未初始化的對象。這就是為什么你看到 null。
例如,當您創建 AA sun = new A();然后打印它時,您從未運行過測試或為 arr 分配了值。print(sun.arr[0]);
編輯原始類后。
System.out.println(sun.month[0]);month正在類中訪問A,但由于您沒有調用Select方法(應以小寫字母開頭),因此您永遠不會用數據填充數組月份。
嘗試這個:
public class A {
String [] month = new String[4];
public String[] select(int pick)
{
switch(pick)
{
case 1:
month[0]="January";
break;
case 2:
month[0]="February";
break;
case 3:
month[0]="March";
break;
case 4:
month[0]="April";
break;
}
return month;
}
公共靜態無效主(字符串[] args){
A sun = new A();
B moon = new B();
C star = new C();
sun.select(1);
System.out.println(sun.month[0]); //printing January
moon.normal_class(); //you are not printing anything here
star.child_class(); //you are not printing anything here
}
添加回答
舉報