java 定义一个Animal抽象含一个抽象方法makeSound()和一个具 体方法move(),分别音移动。定义Bird类和Fish类分别继承 Animal类,并实现自己特有的发声方法的测试类
时间: 2023-06-19 22:04:17 浏览: 106
下面是实现上述要求的Java代码:
Animal.java
```java
public abstract class Animal {
public abstract void makeSound();
public void move() {
System.out.println("移动");
}
}
```
Bird.java
```java
public class Bird extends Animal {
@Override
public void makeSound() {
System.out.println("鸟儿发出啾啾的声音");
}
}
```
Fish.java
```java
public class Fish extends Animal {
@Override
public void makeSound() {
System.out.println("鱼儿不发声音");
}
}
```
Test.java
```java
public class Test {
public static void main(String[] args) {
Animal bird = new Bird();
bird.makeSound();
bird.move();
Animal fish = new Fish();
fish.makeSound();
fish.move();
}
}
```
运行结果:
```
鸟儿发出啾啾的声音
移动
鱼儿不发声音
移动
```
阅读全文