2.定义一个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方法
时间: 2023-12-10 10:36:53 浏览: 84
以下是Ant类的代码实现:
```java
public class Ant extends Insect implements IAttack {
public Ant(String name, int age) {
super(name, age);
}
@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);
a.move();
a.attack();
a.run();
a.grow();
}
}
```
在main函数中,我们创建了一个Ant实例对象a,并调用了其move、attack、run和grow方法。运行程序,输出结果如下:
```
Ant move slowly
Ant bite people
Ant walk step by step
Insect is growing...
```