有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法将车辆添加到阵列列表中

我有四节课。。出租车,汽车,出租车和班车
我乘出租车和班车继承了汽车阶级的遗产。。所以现在我不想再增加一辆穿梭巴士和出租车,我只想增加一辆汽车。。但是我得到一个错误,说类中的构造函数vehicle不能用于给定类型。。我不知道出了什么问题,因为我所做的只是修改了add-taxi方法,效果很好

出租车联班

import java.util.*;
public class TaxiCo
{
    // The name of this company.
    private String companyName;
    // The name of the company's base.
    private final String base;    
    // The fleet of taxis.
    private ArrayList<Vehicle> VehicleFleet;
    // A value for allocating taxi ids.
    private int nextID;
    // A list of available destinations for shuttles.
    private ArrayList<String> destinations;

    /**
     * Constructor for objects of class TaxiCo.
     * @param name The name of this company.
     */
    public TaxiCo(String name)
    {
        companyName = name;
        base = "base";
        VehicleFleet = new ArrayList<Vehicle>();
        nextID = 1;
        destinations = new ArrayList<String>();
        fillDestinations();
    }

    /**
     * Record that we have a new Vehicle.
     * A unique ID will be allocated.
     */
     public void addVehicle()
    {
        Vehicle vehicle = new Vehicle(base, "Vehicle #" + nextID);
        taxiFleet.add(vehicle);
        // Increment the ID for the next one.
        nextID++;
    }

    /**
     * Return the Vehicle with the given id.
     * @param id The id of the Vehicle to be returned.
     * @return The matching Vehicle, or null if no match is found.
     */
    public Vehicle lookup(String id)
    {
        boolean found = false;
        Vehicle Vehicle = null;
        Iterator<Vehicle> it = VehicleFleet.iterator();
        while(!found && it.hasNext()) {
            Vehicle = it.next();
            if(id.equals(Vehicle.getID())) {
                found = true;
            }
        }
        if(found) {
            return Vehicle;
        }
        else {
            return null;
        }
    }

    /**
     * Show the status of the Vehicle fleet. 
     */
    public void showStatus()
    {
        System.out.println("Current status of the " + companyName + " fleet");
        for(Vehicle Vehicle : VehicleFleet) {
            System.out.println(Vehicle.getStatus());
        }
    }

    /**
     * Put all the possible shuttle destinations in a list.
     */
    private void fillDestinations()
    {
        destinations.add("Canterbury West");
        destinations.add("Canterbury East");
        destinations.add("The University");
        destinations.add("Whitstable");
        destinations.add("Herne Bay");
        destinations.add("Sainsbury's");
        destinations.add("Darwin");
        destinations.add("Keynes");
    }
}

共 (1) 个答案

  1. # 1 楼答案

    在来自TaxiCo类的addVehicle()方法中,您将2String传递给构造函数,而您仅使用1String作为参数来定义它

    public void addVehicle()
    {
        Vehicle vehicle = new Vehicle(base, "Vehicle #" + nextID); // BAD! Calling with 2 Strings
        taxiFleet.add(vehicle);
        // Increment the ID for the next one.
        nextID++;
    }
    

    您必须更改它,所以只需将其作为参数传递1 String。比如:

    Vehicle vehicle = new Vehicle("Just one STRING!");
    

    Edit:您也在使用方法addTaxi中的Taxi构造函数来执行此操作