定义一个Ant类,要求如下 (1)继承Insect,实现接口IAttack (2)实现move()方法:打印输出"Ant move slowly"; (3)实现attack()方法:打印输出"Ant bite people"; (4)重写父类的run()方法,打印输出"Ant walk step by step"; (5)在main函数new一个Ant实例对象,赋值给对象a, Ant a = new Ant(…) 调用move和attack、run、grow方法
时间: 2024-01-17 08:05:25 浏览: 89
好的,以下是Ant类的实现代码:
```java
public class Ant extends Insect implements IAttack {
public Ant(int age, String color) {
super(age, color);
}
@Override
public void move() {
System.out.println("Ant move slowly");
}
@Override
public void attack() {
System.out.println("Ant bite people");
}
@Override
public void run() {
System.out.println("Ant walk step by step");
}
public static void main(String[] args) {
Ant a = new Ant(1, "black");
a.move();
a.attack();
a.run();
a.grow();
}
}
```
在main函数中,我们实例化了一个Ant对象,并调用了它的move、attack、run和grow方法。输出结果如下:
```
Ant move slowly
Ant bite people
Ant walk step by step
Insect is growing...
```
阅读全文