我只是测试一些继承,但是似乎我的方法没有被调用,甚至没有被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);
}
}
}
答案 0 :(得分:0)
正确的代码:
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);
}
}
}