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

发布于 2025-01-14 06:52:00 字数 1525 浏览 3 评论 0原文

如果我有一个超类,我们称其为 Car,构造函数参数为 String name、String color、doublewheelSize,而它的子类则称为 Truck,构造函数参数为 String name、String color、doublewheelSize 和 double TruckBedArea,在子类(Truck)中,我有一个名为modifyCar的方法,其参数为Car car、String newName、String newColor、double newWheelSize和double newTruckBedArea,如何找到一种方法来获取该汽车对象并指定它确实是一辆卡车,以便我可以使用卡车设置器 (setTruckBedArea) 来设置新的卡车床区域?这个例子与我的实际分配没有很好的比较,但我的超类(汽车)有一个名为“Car”对象的“ArrayList cars”的 ArrayList 字段,我需要找到一种方法来更改该“Car”对象在这个 ArrayList 字段中,我已经找到了一种方法。我只是循环遍历“Cars”的 ArrayList 中的每个项目,直到它等于作为参数放入的 Car 实例,如果是,那么我会说“cars.get(i).//setter”(本质上) 。但是,如果我说“cars.get(i).setTruckBedArea(newTruckBedArea)”,它将不起作用。我不确定如何将这个汽车列表中的汽车对象专门转换为卡车,因此我可以访问我想要使用的设置器。主要问题是我需要实现一个接口(我们称之为“车辆”),其中 ArrayList 汽车必须是汽车类型,因为它被指定为车辆接口中的类型(否则我只需更改 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
            }
        }
    }
}

If I have a superclass, let's call it Car, with the constructor parameters String name, String color, double wheelSize, and a subclass of this, let's call it Truck, with the constructor parameters String name, String color, double wheelSize, and double truckBedArea, and in the subclass (Truck), I have a method called modifyCar with the paramaters Car car, String newName, String newColor, double newWheelSize, and double newTruckBedArea, how can I find a way to take that Car object and specify that it is indeed a Truck, so I can then use a Truck setter (setTruckBedArea) to set the new truck bed area? This example isn't a great comparison to my actual assignment, but I have an ArrayList field of my superclass (Cars) called "ArrayList cars" of "Car" objects, and I need to find a way to change that "Car" object in this ArrayList field, which I have already found a way of doing. I simply loop through each item in the ArrayList of "Cars" until it equals the instance of the Car put in as a parameter, and if it does, I then say "cars.get(i).//setter" (essentially). However, it would not work if I say "cars.get(i).setTruckBedArea(newTruckBedArea)". I am not sure how to cast the Car object within this list of Cars to a Truck specifically, so I can then access the setter I want to use. The main issue is that I am required to implement an interface (let's call it "Vehicle") wherein the ArrayList cars has to be of type cars, since it is specified to be that in the Vehicle interface (otherwise I would just change the ArrayList field to be ArrayList trucks).

Example:

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
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小矜持 2025-01-21 06:52:00

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

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

instanceof 运算符返回一个布尔值,以判断对象是否是给定类型的实例。

As far as I understand the question, you can use "instanceof" operator:

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

instanceof operator returns a boolean value in result of whether an object is an instance of given type or not.

总攻大人 2025-01-21 06:52:00

Vehicle 应该是一个抽象类。

汽车接口

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

车辆抽象类

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;
    }

}

卡车类

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());
    }

Vehicle should be an Abstract class.

Car Interface

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

Vehicle Abstract 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;
    }

}

Truck 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);
            }
        }
    }

}

Run code.

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());
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文