我写了一个函数,必须:
.
public static void addNewVehicle(MainVehicle vehicle, String type){
Scanner reader = new Scanner(System.in);
System.out.println("Please, enter the make of the " + type + ":");
vehicle.setMake(reader.next());
System.out.println("Please, enter the model of the " + type + ":");
vehicle.setModel(reader.next());
System.out.println("Please, enter the year of manufacture:");
vehicle.setYearOfManufacture(reader.nextInt());
if (vehicle instanceof Car){
System.out.println("Pleas, enter the number of doors:");
((Car) vehicle).setNumberOfDoors(reader.nextInt());
System.out.println("Please, enter the fuel type:");
((Car) vehicle).setFuelType(reader.next());
carList.add((Car) vehicle);
} else if (vehicle instanceof MotorCycle) {
System.out.println("Pleas, enter the number of wheels:");
((MotorCycle) vehicle).setNumberOfWheels(reader.nextInt());
System.out.println("Please, enter the type of the motorcycle");
((MotorCycle) vehicle).setMotorCycleType(reader.next());
motorCyclesList.add((MotorCycle) vehicle);
} else if (vehicle instanceof Truck) {
System.out.println("Pleas, enter the cargo capacity in tons:");
((Truck) vehicle).setCargoCapacityTons(reader.nextInt());
System.out.println("Please, enter the transmission type:");
((Truck) vehicle).setTransmissionType(reader.next());
truckList.add((Truck) vehicle);
System.out.println(truckList.get(0).getMake());
}
我面临的问题是: 当我添加多个对象并尝试打印所有对象的数据时,我得到了我添加了很多条的最后一个对象的数据。
例如:
如果我添加具有以下数据的汽车:
1 1 1 1,0 1
并添加另一辆车并包含以下数据:
2 2 2 2,0 2
如果我要求程序打印出第一个对象,我期望得到:
1 1 1 1,0 1
但我实际得到的是:
2 2 2 2,0 2
如果我要求程序循环处理项目并获取所有数据,我期望这样:
1 1 1 1,0 1
2 2 2 2,0 2
但我明白了:
2 2 2 2,0 2
2 2 2 2,0 2
我尝试做的事情:
我不知道这个问题有什么关系。 我认为问题与此功能有关,但如果不是,请告诉我,以便我提供更多代码。
感谢您的帮助!
要解决此问题,每次添加新车辆时,您都需要创建 MainVehicle(及其子类,如 Car、MotorCycle、Truck)的新实例。这是修改后的函数:
导入java.util.Scanner; 导入java.util.List; 导入 java.util.ArrayList;
公共类VehicleManager { // 存储不同类型车辆的列表 私有静态列表 carInventory = new ArrayList<>(); 私有静态列表 motorCycleInventory = new ArrayList<>(); 私有静态列表 TruckInventory = new ArrayList<>();
public static void addNewVehicle(String category) { Scanner input = new Scanner(System.in); // Create a new instance of the vehicle based on the category MainVehicle newVehicle; if ("Car".equalsIgnoreCase(category)) { newVehicle = new Car(); } else if ("MotorCycle".equalsIgnoreCase(category)) { newVehicle = new MotorCycle(); } else if ("Truck".equalsIgnoreCase(category)) { newVehicle = new Truck(); } else { System.out.println("Sorry, we don't recognize that vehicle category."); return; } // Set the common properties of the vehicle System.out.println("Enter the make of the " + category + ":"); newVehicle.setMake(input.next()); System.out.println("Enter the model of the " + category + ":"); newVehicle.setModel(input.next()); System.out.println("Enter the year it was manufactured:"); newVehicle.setYearOfManufacture(input.nextInt()); // Set the specific properties based on the vehicle type if (newVehicle instanceof Car) { Car car = (Car) newVehicle; System.out.println("Enter the number of doors:"); car.setNumberOfDoors(input.nextInt()); System.out.println("Enter the type of fuel it uses:"); car.setFuelType(input.next()); carInventory.add(car); } else if (newVehicle instanceof MotorCycle) { MotorCycle motorCycle = (MotorCycle) newVehicle; System.out.println("Enter the number of wheels:"); motorCycle.setNumberOfWheels(input.nextInt()); System.out.println("Enter the type of motorcycle:"); motorCycle.setMotorCycleType(input.next()); motorCycleInventory.add(motorCycle); } else if (newVehicle instanceof Truck) { Truck truck = (Truck) newVehicle; System.out.println("Enter the cargo capacity in tons:"); truck.setCargoCapacityTons(input.nextInt()); System.out.println("Enter the type of transmission:"); truck.setTransmissionType(input.next()); truckInventory.add(truck); } } }
如果每次都重用同一个车辆实例,Java 默认情况下不会保证不变性。这是因为列表中的内容是对堆中对象的引用,而不是该对象的新副本。
要解决该问题,您必须创建唯一的车辆实例(使用
new Car()
等),设置从输入读取的参数,并将每个实例添加到列表中。
另请参阅: