声明一个抽象类 Driver 及两个子类 FemaleDriver 及 MaleDriver 在 Diver 类中声明了抽象方法 drives ,在两个子类中对这个方法进行了重写。:运行结果 A Female driver drives a vehicle . A male driver drives a vehicle .
时间: 2024-03-01 10:56:27 浏览: 36
好的,这是一个简单的面向对象编程问题。下面是对应的 Java 代码:
```java
public abstract class Driver {
public abstract void drives();
}
public class FemaleDriver extends Driver {
@Override
public void drives() {
System.out.println("A Female driver drives a vehicle.");
}
}
public class MaleDriver extends Driver {
@Override
public void drives() {
System.out.println("A male driver drives a vehicle.");
}
}
```
在 main 函数中,我们可以这样调用:
```java
public static void main(String[] args) {
Driver femaleDriver = new FemaleDriver();
Driver maleDriver = new MaleDriver();
femaleDriver.drives(); // 输出 "A Female driver drives a vehicle."
maleDriver.drives(); // 输出 "A male driver drives a vehicle."
}
```
这样就可以得到输出结果了。
相关推荐
![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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)