亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么我的方法在主要方法中沒有被檢測到?

為什么我的方法在主要方法中沒有被檢測到?

慕碼人2483693 2022-11-10 15:11:28
我只是在測試一些繼承,但似乎我的方法沒有被調用,甚至沒有被 main 方法看到。它編譯,但只是說文件中沒有檢測到方法。我的代碼有什么問題?public class monkey{    public void main(String[] args){          Fruit jeff = new Fruit("ree");          Fruit mike = new Apple("ree");          jeff.talk();          mike.talk();    }class Fruit {     String sound;    public Fruit(String s) {       sound = s;    }     public void talk(){      System.out.print(sound);    }} class Apple extends Fruit {    public Apple(String s){      super(s);   }}}
查看完整描述

1 回答

?
蝴蝶不菲

TA貢獻1810條經驗 獲得超4個贊

  • 將靜態放在主要方法簽名中。

  • 創建靜態內部類,因為您想在靜態 main 方法中訪問這些類。

正確代碼:

public class monkey {

    public static void main(String[] args) {

        Fruit jeff = new Fruit("ree");

        Fruit mike = new Apple("ree");


        jeff.talk();

        mike.talk();

    }


    static class  Fruit {

        String sound;


        public Fruit(String s) {

            sound = s;

        }


        public void talk() {

            System.out.print(sound);

        }

    }


    static class Apple extends Fruit {

        public Apple(String s) {

            super(s);

        }

    }

}


查看完整回答
反對 回復 2022-11-10
  • 1 回答
  • 0 關注
  • 94 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號