为什么我的方法在主要方法中没有被检测到?

我只是在测试一些继承,但似乎我的方法没有被调用,甚至没有被 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);

   }

}

}


慕码人2483693
浏览 60回答 1
1回答

蝴蝶不菲

将静态放在主要方法签名中。创建静态内部类,因为您想在静态 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);        }    }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java