如何将对象(超类类型)的 ArrayList 的对象转换为子类类型的对象

How to cast an object of an ArrayList of objects (of type superclass) to an object of type subclass

如果我有一个超类,我们称之为 Car,构造函数参数为 String name、String color、double wheelSize,以及它的子类,我们称之为 Truck,构造函数参数为 String name、String color、双 wheelSize 和双 truckBedArea,在子类 (Truck) 中,我有一个名为 modifyCar 的方法,参数为 Car car、String newName、String newColor、double newWheelSize 和 double newTruckBedArea,我怎样才能找到一种方法来获取那辆 Car对象并指定它确实是一辆卡车,这样我就可以使用卡车 setter (setTruckBedArea) 来设置新的卡车床区域?这个例子与我的实际任务没有很好的比较,但我有一个名为“Car”对象的“ArrayList cars”的超类(Cars)的 ArrayList 字段,我需要找到一种方法来更改那个“Car”对象在这个 ArrayList 字段中,我已经找到了一种方法。我简单地遍历“Cars”的 ArrayList 中的每个项目,直到它等于作为参数放入的 Car 的实例,如果是,我然后说“cars.get(i).//setter”(本质上)。但是,如果我说“cars.get(i).setTruckBedArea(newTruckBedArea)”,它将不起作用。我不确定如何将此 Cars 列表中的 Car 对象具体转换为 Truck,这样我就可以访问我想使用的 setter。主要问题是我需要实现一个接口(我们称之为“Vehicle”),其中 ArrayList cars 必须是汽车类型,因为它被指定为 Vehicle 接口中的那种(否则我只会更改 ArrayList字段为 ArrayList 卡车)。

示例:

public class Truck implements Vehicle { //have to implement this interface
    //... other fields
    private ArrayList<Car> cars;
    //... other methods/constructors
    public void modifyCar(Car car, String newName, String newColor, double newWheelSize, double newTruckBedArea) { 
//have to have "Car car" as parameter for this method because of interface
        for (int i = 0; i < cars.size(); i++) {
            if (cars.get(i).equals(car)) {
                cars.get(i).setColor(newColor);
                cars.get(i).setName(newName);
                cars.get(i).setWheelSize(newWheelSize);
                cars.get(i).setTruckBedArea(newTruckBedArea); //will produce error
            }
        }
    }
}

据我了解这个问题,你可以使用“instanceof”运算符:

   if(cars.get(i) instanceof Truck){
       Truck truck = (Truck) cars.get(i);
       truck.setTruckBedArea(newTruckBedArea);
   }

instanceof 运算符returns 一个布尔值,表示对象是否是给定类型的实例。

车辆应该是抽象的 class。

车载界面

public interface Car {
    void modifyCar(Car car, String newName, String newColor, double newWheelSize, double newTruckBedArea);
}

车辆摘要Class

public abstract class Vehicle implements Car {

    String name;
    String color;
    double wheelSize;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public double getWheelSize() {
        return wheelSize;
    }

    public void setWheelSize(double wheelSize) {
        this.wheelSize = wheelSize;
    }

}

卡车Class

public class Truck extends Vehicle {

    double truckBedArea;
    private ArrayList<Car> cars;

    public double getTruckBedArea() {
        return truckBedArea;
    }

    public void setTruckBedArea(double truckBedArea) {
        this.truckBedArea = truckBedArea;
    }

    public ArrayList<Car> getCars() {
        return cars;
    }

    public void setCars(ArrayList<Car> cars) {
        this.cars = cars;
    }

    @Override
    public void modifyCar(Car car, String newName, String newColor, double newWheelSize, double newTruckBedArea) {
        for (int i = 0; i < cars.size(); i++) {
            if (cars.get(i).equals(car)){
                ((Vehicle)cars.get(i)).setColor(newColor);
                ((Vehicle)cars.get(i)).setWheelSize(newWheelSize);
                ((Truck)cars.get(i)).setTruckBedArea(newTruckBedArea);
            }
        }
    }

}

运行代码。

public static void main(String[] args) {
        // TODO code application logic here
        Truck trucks = new Truck();
        trucks.setColor("Red");
        trucks.setName("Nissan");
        trucks.setWheelSize(20.15);
        trucks.setTruckBedArea(3.5);
        ArrayList<Car> cars = new ArrayList<Car>();
        cars.add(trucks);
        trucks.setCars(cars);
        
        trucks.modifyCar(trucks, "Kia", "Blue", 15.5, 14.0);

        System.out.println(trucks.getTruckBedArea());
    }