我被要求创建一个主方法,允许我创建一个狼对象,鹦鹉对象和犀牛对象,我已经为每个动物创建了类,并且它们扩展了包含抽象方法makeNoise()的抽象类动物.我在我的rhino,parrot和wolf类中实现了这个抽象方法,该方法包含一个System.out.println函数和与这些动物相关的噪声.例如,我的parrot类(扩展动物)包含一个方法makeNoise(),它打印出"squawk".
我被要求证明我可以为主要课程中的每个动物对象调用这个makeNoise方法,我该怎么做呢?
public class Main() {
Wolf myWolf = new Wolf();
//I want my wolf to make a noise here
Parrot myParrot = new Parrot();
//I want my parrot to make a noise here
Rhino myRhino = new Rhino();
//I want my rhino to make a noise here
}
Run Code Online (Sandbox Code Playgroud)