抽象类Vehicle,非抽象子类Car类,均实现接口ProhibitDriving ProhibitDriving方法:boolean prohibitDriving(enum Day) Vehicle属性:车牌号;实现prohibitDriving方法:比如周一限行尾号0,5 Car继承父类Vehicle:若干构造方法(Car属性自定义),getter,setter 写测试类carTest,声明对象myCar,设置车牌;声明接口变量goOut(输入今日周几)用goOut判断是否是限行
时间: 2023-12-03 18:42:14 浏览: 66
以下是符合题目要求的Java代码:
```java
public interface ProhibitDriving {
boolean prohibitDriving(Day day);
}
public enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;
}
public abstract class Vehicle implements ProhibitDriving {
private String licensePlate;
public Vehicle(String licensePlate) {
this.licensePlate = licensePlate;
}
public String getLicensePlate() {
return licensePlate;
}
public void setLicensePlate(String licensePlate) {
this.licensePlate = licensePlate;
}
@Override
public abstract boolean prohibitDriving(Day day);
}
public class Car extends Vehicle {
private String brand;
private String color;
private int maxSpeed;
public Car(String licensePlate, String brand, String color, int maxSpeed) {
super(licensePlate);
this.brand = brand;
this.color = color;
this.maxSpeed = maxSpeed;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getMaxSpeed() {
return maxSpeed;
}
public void setMaxSpeed(int maxSpeed) {
this.maxSpeed = maxSpeed;
}
@Override
public boolean prohibitDriving(Day day) {
switch (day) {
case MONDAY:
return getLicensePlate().endsWith("0") || getLicensePlate().endsWith("5");
case TUESDAY:
case WEDNESDAY:
case THURSDAY:
case FRIDAY:
case SATURDAY:
case SUNDAY:
default:
return false;
}
}
}
public class carTest {
public static void main(String[] args) {
Car myCar = new Car("京A12345", "宝马", "黑色", 240);
ProhibitDriving goOut = myCar::prohibitDriving;
System.out.println("今天是否限行:" + goOut.prohibitDriving(Day.MONDAY));
}
}
```
在测试类中,我们创建了一个Car对象myCar,并将其转换为ProhibitDriving接口的实现。接着,我们声明了一个ProhibitDriving类型的变量goOut,并将myCar的prohibitDriving方法赋值给它,这样就可以通过goOut来调用prohibitDriving方法了。最后,我们调用goOut的prohibitDriving方法,传入今天是周几的枚举值,即可判断是否限行。
阅读全文