按要求编写一个Java应用程序程序: (1)定义一个接口CanFly,描述会飞的方法public void fly(); (2)分别定义类飞机和鸟,实现CanFly接口。 (3)定义一个测试类,测试飞机和鸟,在main方法中创建飞机对象和鸟对象, 再定义一个makeFly()方法,其中让会飞的事物飞。并在main方法中调用该方法, 让飞机和鸟起飞。
时间: 2023-06-27 15:04:53 浏览: 321
下面是按要求编写的Java应用程序:
```java
// CanFly接口
interface CanFly {
void fly();
}
// 飞机类,实现CanFly接口
class Airplane implements CanFly {
public void fly() {
System.out.println("飞机起飞!");
}
}
// 鸟类,实现CanFly接口
class Bird implements CanFly {
public void fly() {
System.out.println("鸟儿飞翔!");
}
}
// 测试类
public class TestFly {
public static void main(String[] args) {
// 创建飞机对象和鸟对象
Airplane airplane = new Airplane();
Bird bird = new Bird();
// 让会飞的事物飞
makeFly(airplane);
makeFly(bird);
}
// 让会飞的事物飞的方法
public static void makeFly(CanFly flyable) {
flyable.fly();
}
}
```
输出结果:
```
飞机起飞!
鸟儿飞翔!
```
相关问题
定义一个接口CanFly,描述会飞的方法public void fly();分别定义类飞机和鸟,实现CanFly接口。定义一个测试类,测试飞机和鸟,在main方法中创建飞机对象和鸟对象,让飞机和鸟起飞。
好的,以下是Java程序代码:
```
interface CanFly {
void fly();
}
class Airplane implements CanFly {
public void fly() {
System.out.println("飞机起飞!");
}
}
class Bird implements CanFly {
public void fly() {
System.out.println("鸟儿飞翔!");
}
}
public class Test {
public static void main(String[] args) {
CanFly airplane = new Airplane();
CanFly bird = new Bird();
airplane.fly();
bird.fly();
}
}
```
程序输出:
```
飞机起飞!
鸟儿飞翔!
```
在上述代码中,我们定义了接口CanFly,描述了会飞的方法fly()。接着我们定义了类Airplane和Bird,并且都实现了CanFly接口中的fly()方法。最后,在测试类Test中,我们创建了Airplane和Bird对象,并且让它们都起飞。最终程序输出了飞机起飞和鸟儿飞翔的信息。
定义一个接口CanFly,接口中声明fly()方法,再分别定义Bird(鸟)类,B utterfly(蝴蝶)类,Plane(飞机)类实现CanFly接口,最后在测试类中用多态执行三个实现类的fly方法
好的,下面是代码实现:
```java
// 定义CanFly接口
interface CanFly {
void fly();
}
// Bird类实现CanFly接口
class Bird implements CanFly {
@Override
public void fly() {
System.out.println("鸟儿飞行中...");
}
}
// Butterfly类实现CanFly接口
class Butterfly implements CanFly {
@Override
public void fly() {
System.out.println("蝴蝶飞行中...");
}
}
// Plane类实现CanFly接口
class Plane implements CanFly {
@Override
public void fly() {
System.out.println("飞机飞行中...");
}
}
// 测试类
public class FlyTest {
public static void main(String[] args) {
CanFly bird = new Bird();
CanFly butterfly = new Butterfly();
CanFly plane = new Plane();
bird.fly();
butterfly.fly();
plane.fly();
}
}
```
输出结果:
```
鸟儿飞行中...
蝴蝶飞行中...
飞机飞行中...
```
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)