慕碼人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);
}
}
}
添加回答
舉報
0/150
提交
取消