(6) 用java代码定义机动车接口(Automobile)和非机动车接口(Nonautomobile),分别包含表示其运动模式的抽象方法;编写总的“车”类(VehicleClass),其中描述车名、车轮数,以及机动车和非机动车变量,该类实现机动车和非机动车接口;编写继承“车”类的公共汽车类(BusClass)和自行车类(BicycleClass)。
时间: 2023-05-22 20:07:20 浏览: 190
好的,我会扮演一个由 CSDN 开发的C知道,接下来我将为您详细回答问题。
首先,我们定义一个机动车接口(Automobile)和一个非机动车接口(Nonautomobile),代码如下:
```java
public interface Automobile {
void drive();
}
public interface Nonautomobile {
void pedal();
}
```
其中,机动车接口包含一个表示运动模式的抽象方法 drive(),非机动车接口包含一个表示运动模式的抽象方法 pedal()。
接下来,我们定义一个总的“车”类(VehicleClass),代码如下:
```java
public class VehicleClass implements Automobile, Nonautomobile {
private String name; // 车名
private int numOfWheels; // 车轮数
private Automobile automobile; // 机动车变量
private Nonautomobile nonautomobile; // 非机动车变量
public VehicleClass(String name, int numOfWheels, Automobile automobile, Nonautomobile nonautomobile) {
this.name = name;
this.numOfWheels = numOfWheels;
this.automobile = automobile;
this.nonautomobile = nonautomobile;
}
public void drive() {
automobile.drive();
}
public void pedal() {
nonautomobile.pedal();
}
public String getName() {
return name;
}
public int getNumOfWheels() {
return numOfWheels;
}
}
```
其中,VehicleClass类实现了机动车接口和非机动车接口,包含车名(name)、车轮数(numOfWheels)、机动车变量(automobile)和非机动车变量(nonautomobile),并且实现了两个接口的方法。
接下来,我们定义继承“车”类的公共汽车类(BusClass),代码如下:
```java
public class BusClass extends VehicleClass implements Automobile {
public BusClass(String name, int numOfWheels, Automobile automobile, Nonautomobile nonautomobile) {
super(name, numOfWheels, automobile, nonautomobile);
}
public void drive() {
System.out.println("公共汽车启动,开始行驶!");
}
}
```
BusClass类继承了VehicleClass类,实现了机动车接口,并重写了drive()方法。
最后,我们定义继承“车”类的自行车类(BicycleClass),代码如下:
```java
public class BicycleClass extends VehicleClass implements Nonautomobile {
public BicycleClass(String name, int numOfWheels, Automobile automobile, Nonautomobile nonautomobile) {
super(name, numOfWheels, automobile, nonautomobile);
}
public void pedal() {
System.out.println("自行车开始踩踏板,开始行驶!");
}
}
```
BicycleClass类同样继承了VehicleClass类,实现了非机动车接口,并重写了pedal()方法。
希望我的回答能够帮助到您!
相关推荐
![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)
![](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)