2、按以下要求编写程序 (1) 编写Animal接口,接口中声明run() 方法 (2) 定义Bird类和Fish类分别实现Animal接口中的run()方法 分别输出 “我会飞”和“我会游” (3) 编写Bird类和Fish类的测试程序,并调用其中的run()方法
时间: 2024-03-20 12:43:28 浏览: 20
好的,以下是符合你要求的 Java 代码:
Animal 接口:
```
public interface Animal {
public void run();
}
```
Bird 类实现 Animal 接口:
```
public class Bird implements Animal {
public void run() {
System.out.println("我会飞");
}
}
```
Fish 类实现 Animal 接口:
```
public class Fish implements Animal {
public void run() {
System.out.println("我会游");
}
}
```
Bird 和 Fish 的测试程序:
```
public class AnimalTest {
public static void main(String[] args) {
Animal bird = new Bird();
Animal fish = new Fish();
bird.run(); // 输出:"我会飞"
fish.run(); // 输出:"我会游"
}
}
```
希望这份代码能够帮到你!
相关问题
按以下要求编写程序 (1) 编写Animal接口,接口中声明run() 方法 (2) 定义Bird类和Fish类实现Animal接口 (3) 编写Bird类和Fish类的测试程序,并调用其中的run()方法
代码如下:
```java
// Animal接口
interface Animal {
void run();
}
// Bird类实现Animal接口
class Bird implements Animal {
@Override
public void run() {
System.out.println("Bird is flying in the sky.");
}
}
// Fish类实现Animal接口
class Fish implements Animal {
@Override
public void run() {
System.out.println("Fish is swimming in the water.");
}
}
// 测试程序
public class Test {
public static void main(String[] args) {
// 创建Bird对象并调用run()方法
Animal bird = new Bird();
bird.run();
// 创建Fish对象并调用run()方法
Animal fish = new Fish();
fish.run();
}
}
```
运行结果如下:
```
Bird is flying in the sky.
Fish is swimming in the water.
```
按以下要求编写程序:编写Animal接口,接口中声明run() 方法;定义Bird类和Fish类实现Animal接口;编写Bird类和Fish类的测试程序,并调用其中的run()方法。
以下是一个可能的实现,包括Animal接口和Bird、Fish类及其测试程序:
```java
// Animal接口
public interface Animal {
void run();
}
// Bird类实现Animal接口
public class Bird implements Animal {
@Override
public void run() {
System.out.println("Bird is flying!");
}
}
// Fish类实现Animal接口
public class Fish implements Animal {
@Override
public void run() {
System.out.println("Fish is swimming!");
}
}
// 测试程序
public class TestAnimal {
public static void main(String[] args) {
Animal bird = new Bird();
bird.run(); // 输出:Bird is flying!
Animal fish = new Fish();
fish.run(); // 输出:Fish is swimming!
}
}
```
在测试程序中,我们分别创建了一个Bird对象和一个Fish对象,并调用它们的run()方法。由于Bird和Fish类都实现了Animal接口,因此它们都必须提供run()方法的实现,这里分别输出了"Bird is flying!"和"Fish is swimming!"。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)