第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

將字符串?dāng)?shù)組返回給其他類(包括繼承)并打印

將字符串?dāng)?shù)組返回給其他類(包括繼承)并打印

躍然一笑 2023-10-13 09:59:48
我在將數(shù)組返回到 java 中的其他類或繼承子類時(shí)遇到問(wèn)題。A級(jí)import java.util.Scanner; public class A {  String [] month = new String[4];    Scanner sc = new Scanner(System.in); public String[] Select()  {        System.out.println("choose");           int pick=sc.nextInt();        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;}}C級(jí)    public class C extends A{     public void child_class()    {        System.out.println(month[0]);   }        }B級(jí)public class B {    A  select = new A ();    public void normal_class()    {               System.out.println(select.month[0]);    } }主類 公共類 main {public static void main(String[] args) {        A sun = new A();        B moon = new B();        C star = new C();        System.out.println(sun.month[0]);                   moon.normal_class(); //        star.child_class();        }}結(jié)果nullnullnull我想將數(shù)組的結(jié)果返回給類或子類。如果我在A類中選擇案例1,我想從類中獲取結(jié)果“January”
查看完整描述

1 回答

?
嗶嗶one

TA貢獻(xiàn)1854條經(jīng)驗(yàn) 獲得超8個(gè)贊

請(qǐng)更正并完成問(wèn)題中的類,將其復(fù)制到編輯器中,并確保使用預(yù)覽時(shí)它們看起來(lái)正確。我不明白B 類中的testinprint(test.arr[0]);來(lái)自哪里。但是,您正在打印尚未初始化的對(duì)象。這就是為什么你看到 null。


例如,當(dāng)您創(chuàng)建 AA sun = new A();然后打印它時(shí),您從未運(yùn)行過(guò)測(cè)試或?yàn)?arr 分配了值。print(sun.arr[0]);


編輯原始類后。


System.out.println(sun.month[0]);month正在類中訪問(wèn)A,但由于您沒(méi)有調(diào)用Select方法(應(yīng)以小寫(xiě)字母開(kāi)頭),因此您永遠(yuǎn)不會(huì)用數(shù)據(jù)填充數(shù)組月份。


嘗試這個(gè):


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;

}

公共靜態(tài)無(wú)效主(字符串[] 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

    }


查看完整回答
反對(duì) 回復(fù) 2023-10-13
  • 1 回答
  • 0 關(guān)注
  • 107 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)