VehicleData 类定义如下:
public class VehicleData {
private String model;
private String color;
private int year;
private String engine;
public VehicleData() {
this.model = "none";
this.color = "none";
this.year = 0;
this.engine = "none";
}
public VehicleData(String model, String color, int year, String engine) {
this.model = model;
this.color = color;
this.year = year;
this.engine = engine;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public String getEngine() {
return engine;
}
public void setEngine(String engine) {
this.engine = engine;
}
}
TestVehicleData 类定义如下:
public class TestVehicleData {
public static void main(String[] args) {
VehicleData vehicleData1 = new VehicleData();
VehicleData vehicleData2 = new VehicleData();
vehicleData2.setModel("XC40");
vehicleData2.setColor("red");
vehicleData2.setYear(2021);
vehicleData2.setEngine("all-electric");
VehicleData vehicleData3 = new VehicleData();
vehicleData3.setModel("XC60");
vehicleData3.setColor("blue");
vehicleData3.setYear(2022);
vehicleData3.setEngine("petrol");
for (VehicleData vehicleData : Arrays.asList(vehicleData1, vehicleData2, vehicleData3)) {
System.out.println("The car is " + vehicleData.getModel() + " " + vehicleData.getColor() + " " + vehicleData.getYear() + " " + vehicleData.getEngine());
}
}
}
TestVehicleData 运行结果如下:

Rental 类定义如下:
public class Rental {
private int customerld;
private int daysNum;
private VehicleData vehicleData;
public Rental() {
this.customerld = 0;
this.daysNum = 0;
this.vehicleData = new VehicleData();
this.vehicleData.setYear(0);
}
public Rental(int customerld, int daysNum, VehicleData vehicleData) {
this.customerld = customerld;
this.daysNum = daysNum;
this.vehicleData = vehicleData;
}
public int getCustomerld() {
return customerld;
}
public void setCustomerld(int customerld) {
this.customerld = customerld;
}
public int getDaysNum() {
return daysNum;
}
public void setDaysNum(int daysNum) {
this.daysNum = daysNum;
}
public VehicleData getVehicleData() {
return vehicleData;
}
public void setVehicleData(VehicleData vehicleData) {
this.vehicleData = vehicleData;
}
}
TestRent 类定义如下:
public class TestRent {
public static void main(String[] args) {
Rental rental1 = new Rental();
VehicleData vehicleData = new VehicleData();
vehicleData.setModel("XC60");
vehicleData.setColor("silver");
vehicleData.setYear(2021);
vehicleData.setEngine("hybrid");
Rental rental2 = new Rental(12345, 5, vehicleData);
vehicleData = new VehicleData();
vehicleData.setModel("XC40");
vehicleData.setColor("white");
vehicleData.setYear(2018);
vehicleData.setEngine("petrol");
Rental rental3 = new Rental(34567, 2, vehicleData);
for (Rental rental : Arrays.asList(rental1, rental2, rental3)) {
System.out.println("Customer ID# " + rental.getCustomerld() + " has rent a car for: " + rental.getDaysNum() + " days");
System.out.println("\tThe car is " + rental.getVehicleData().getModel() + " " + rental.getVehicleData().getColor() + " "
+ rental.getVehicleData().getYear() + " " + rental.getVehicleData().getEngine());
}
}
}
TestRent 类运行结果如下:
