3.继承多态综合应用练习3 (1)定义一个驾驶员抽象类Driver,它有String sex(性别)和int age(年龄)两个成员属性,还有一个抽象的成员方法drives(Vehicle v),并从它派生出女驾驶员FemaleDriver和男驾驶员MaleDriver两个子类。 (2)定义一个抽象类Vehicle,它有String vtype(车型)和String vcode(车牌号码)两个成员属性,还有一个抽象的成员方法drivedByDriver(Driver who),并从它派生出Bus和Car两个子类。 (3)定义一个测试类DriverTest,在其main方法中先创建fd和md两个男女Driver对象,然后创建vb和vc两个汽车类Vehicle对象。 要求: (1)将以上所有类均放置到com.sy4.exa03包中,然后分别通过调用drives()和drivedByDriver()成员方法输出如下字符串: 一个男司机驾驶一辆小轿车。 一个女司机驾驶一辆公交车。 (2)在com.sy4.exa03.test包中新建一个DriverTest测试类。在该类的main方法中先创建20个Vehicle对象(随机生成车牌号,车牌号范围为1~1000),并规定只有双号车辆能够通过,输出所有车辆的通过情况。 要求:测试类放在包名为com.sy4.exe03.test包中,其它类放在com.sy4.exa03包中。
时间: 2024-02-21 16:59:25 浏览: 141
关于继承和多态的简单练习
抱歉,我作为一个语言模型 AI,不具备编程能力,无法直接为您提供代码。但是,我可以给您提供一份参考代码,供您参考。
Driver.java
```java
package com.sy4.exa03;
public abstract class Driver {
protected String sex;
protected int age;
public Driver(String sex, int age) {
this.sex = sex;
this.age = age;
}
public abstract void drives(Vehicle v);
}
```
FemaleDriver.java
```java
package com.sy4.exa03;
public class FemaleDriver extends Driver {
public FemaleDriver(String sex, int age) {
super(sex, age);
}
@Override
public void drives(Vehicle v) {
System.out.println("一个女司机驾驶一辆" + v.getVtype());
}
}
```
MaleDriver.java
```java
package com.sy4.exa03;
public class MaleDriver extends Driver {
public MaleDriver(String sex, int age) {
super(sex, age);
}
@Override
public void drives(Vehicle v) {
System.out.println("一个男司机驾驶一辆" + v.getVtype());
}
}
```
Vehicle.java
```java
package com.sy4.exa03;
public abstract class Vehicle {
protected String vtype;
protected String vcode;
public Vehicle(String vtype, String vcode) {
this.vtype = vtype;
this.vcode = vcode;
}
public abstract void drivedByDriver(Driver who);
public String getVtype() {
return vtype;
}
public String getVcode() {
return vcode;
}
}
```
Bus.java
```java
package com.sy4.exa03;
public class Bus extends Vehicle {
public Bus(String vtype, String vcode) {
super(vtype, vcode);
}
@Override
public void drivedByDriver(Driver who) {
who.drives(this);
}
}
```
Car.java
```java
package com.sy4.exa03;
public class Car extends Vehicle {
public Car(String vtype, String vcode) {
super(vtype, vcode);
}
@Override
public void drivedByDriver(Driver who) {
who.drives(this);
}
}
```
DriverTest.java
```java
package com.sy4.exa03.test;
import java.util.Random;
import com.sy4.exa03.Bus;
import com.sy4.exa03.Car;
import com.sy4.exa03.Driver;
import com.sy4.exa03.FemaleDriver;
import com.sy4.exa03.MaleDriver;
import com.sy4.exa03.Vehicle;
public class DriverTest {
public static void main(String[] args) {
// Part 1
Driver fd = new FemaleDriver("女", 28);
Driver md = new MaleDriver("男", 30);
Vehicle vc = new Car("小轿车", "粤B12345");
Vehicle vb = new Bus("公交车", "粤B67890");
md.drives(vc);
fd.drives(vb);
// Part 2
Vehicle[] vehicles = new Vehicle[20];
Random r = new Random();
for (int i = 0; i < vehicles.length; i++) {
String vtype = r.nextInt(2) == 0 ? "小轿车" : "公交车";
String vcode = "粤B" + (i + 1);
vehicles[i] = vtype.equals("小轿车") ? new Car(vtype, vcode) : new Bus(vtype, vcode);
}
for (Vehicle v : vehicles) {
if (Integer.parseInt(v.getVcode().substring(2)) % 2 == 0) {
System.out.println(v.getVcode() + "可以通过");
} else {
System.out.println(v.getVcode() + "不能通过");
}
}
}
}
```
阅读全文