酷代码 AI
菜单
全部AI  /  热门  /  AI写代码神器
服务商

题目4(接口) (1)创建一个animal动物类,要求有方法eat()方法,方法输出一条语句“吃东 西 (2)创建一个接口a,接口里有一个抽象方法ny()。 (3)创建一个bird类继承animal 类并实现 接口a里的方法输出一条有语句"鸟 儿飞翔,重写eat()方法输出一条语句“鸟儿吃虫” (4)在test类中创建bird对象,调用eat方法。然后向下转型调用eat()方法、 fly()方法。

以下是使用 Java 语言实现上述需求的代码: ```java class Animal { public void eat() { System.out.println("吃东西"); } } interface A { void ny(); } class Bird extends Animal implements A { @Override public void eat() { System.out.println("鸟儿吃虫"); } @Override public void ny() { System.out.println("鸟儿飞翔"); } } public class Test { public static void main(String[] args) { Bird bird = new Bird(); bird.eat(); // 向下转型 if (bird instanceof A) { A a = (A) bird; a.ny(); a.eat(); } } } ``` [2024-09-19 16:45:48 | AI写代码神器 | 184点数解答]

相关提问