这里真的是Java的新手。我试图建立在动物的抽象类上,并将其扩展到Ducks的子类。然后,我将创建一个对象ducksworth,该对象证明Duck继承了超类动物的方法,并且可以调用对抽象类变量的引用。当我尝试在Netbeans中运行代码时,即使类AllAnimals包含主调用,它也找不到主方法调用。任何帮助,将不胜感激?此代码完全写在Netbeans的单个文件中。
理想情况下,我希望输出的是“羽毛”
abstract class Animal {
String FUR= "fur";
String FEATHER = "feather";
String SCALE = "scale";
String SHELL = "shell";
String SKIN = "skin";
abstract String getCovering( );
abstract void makeSound();
abstract boolean canFly( );
}
class Duck extends Animal {
String covering = FEATHER;
String sound = "quack";
boolean flight = true;
String getCovering() {
System.out.println("Ducks are covered in " + covering);
return covering;
}
void makeSound() {
System.out.println(sound);
}
boolean canFly() {
return flight;
}
}
class allanimals {
public static void main(String[] args) {
Duck ducksworth = new Duck();
ducksworth.getCovering();
}
}
最佳答案
正如大家所说。将实际课程放在下面。
所做的更改:
类名allanimals更改为AllAnimals以获得更好的标准。
AllAnimals类对jvm公开以访问静态main方法。
文件名作为公共类名给出-AllAnimals.java
档案名称:AllAnimals.java
abstract class Animal {
String FUR= "fur";
String FEATHER = "feather";
String SCALE = "scale";
String SHELL = "shell";
String SKIN = "skin";
abstract String getCovering( );
abstract void makeSound();
abstract boolean canFly( );
}
class Duck extends Animal {
String covering = FEATHER;
String sound = "quack";
boolean flight = true;
String getCovering() {
System.out.println("Ducks are covered in " + covering);
return covering;
}
void makeSound() {
System.out.println(sound);
}
boolean canFly() {
return flight;
}
}
public class AllAnimals {
public static void main(String[] args) {
Duck ducksworth = new Duck();
ducksworth.getCovering();
}
}