介绍多态性:用单个数组列表替换两个数组列表(Java)

时间:2016-12-29 17:42:37

标签: java inheritance arraylist polymorphism

对于作业,我正在努力将多态和继承引入现有程序。

我目前遇到的问题是用一个列表替换两个单独的列表。我对Java编程很陌生(阅读:不是那么擅长)所以我很抱歉,如果这还不清楚。

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<Taxi> taxiFleet;
    // The fleet of shuttles.
    private ArrayList<Shuttle> shuttleFleet;
    // The fleet of vehicles.
    private int nextID;
    // A list of available destinations for shuttles.
    private ArrayList<String> destinations;

我需要将ArrayList<Shuttle> shuttleFleetArrayList<Taxi> taxiFleet替换为ArrayList<Vehicle>

当我最初添加ArrayList<Vehicle> vehicleFleet时,我认为我需要创建一个名为addVehicle的新方法,但暂时决定反对它(我会包含该方法的代码,因为有人认为我是走向正确的道路)。

所以我只是添加了以下内容:

字段:

private ArrayList<Vehicle> vehicleFleet;

构造函数:

vehicleFleet = new ArrayList<Vehicle>();

对以下方法进行了更改:

public void addTaxi(Vehicle vehicle)
{
   Vehicle taxi = new Taxi(base, "Car #" + nextID);
   vehicleFleet.add(taxi);
   // Increment the ID for the next one.
   nextID++;
}

......以及addShuttle的类似内容。

编辑: 但是当我尝试编译时,会突出显示以下代码:new Taxi(base, "Car #" + nextID); and I get this error message.

关于如何解决这个问题的想法?如果你认为它能帮助我更好地编写代码或者更好地理解java语言,我也会接受任何批评。谢谢!

这是TaxiCo类的其余代码,如果有帮助的话。

public TaxiCo(String name, String base)
{
    companyName = name;
    base = "base";
    taxiFleet = new ArrayList<Taxi>();
    shuttleFleet = new ArrayList<Shuttle>();
    nextID = 1;
    destinations = new ArrayList<String>();
    fillDestinations();
}

public void addTaxi(Vehicle vehicle)
{
    Taxi taxi = new Taxi(base, "Car #" + nextID);
    taxiFleet.add(taxi);
    // Increment the ID for the next one.
    nextID++;
}     

public void addShuttle()
{
    // Sanity warning:
    // The following is a thoroughly contrived way to create a route!

    // Create a random list of destinations for its route.
    Collections.shuffle(destinations);
    ArrayList<String> route = new ArrayList<String>();
    // The starting point is always the base.
    route.add(base);
    // Decide on an (arbitrary) length for all routes.
    final int ROUTE_LENGTH = 3;
    for(int i = 0; i < ROUTE_LENGTH; i++) {
        route.add(destinations.get(i));
    }

    Shuttle shuttle = new Shuttle("Shuttle #" + nextID, route);
    shuttleFleet.add(shuttle);
    // Increment the ID for the next one.
    nextID++;
}


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;
         }
        else{
            return null;
        }
    }
}

    public void showStatus()
{
    System.out.println("Current status of the " + companyName + " fleet");
    for(Taxi taxi : taxiFleet) {
        System.out.println(taxi.getStatus());
    }
    for(Shuttle shuttle : shuttleFleet) {
        System.out.println(shuttle.getStatus());
    }
}


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

......我之前谈到的addVehicle方法。

public void addVehicle(Vehicle vehicle)
{
    Vehicle taxi = new Taxi(base, "Car #" + nextID);
    //taxiFleet.add(taxi);
    fleet.addVehicle(taxi);
    //Increment the ID for the next one. but do we need two...
    nextID++;

    //Create a random list of destinations for its route.
    Collections.shuffle(destinations);
    ArrayList<String> route = new ArrayList<String>();
    //The starting point is always the base.
    route.add(base);
    //Decide on an arbitrary length for all routes.
    final int ROUTE_LENGTH = 3;
    for(int i = 0; i < ROUTE_LENGTH; i++) {
        route.add(destinations.get(i));
    }

    Vehicle shuttle = new Shuttle("Shuttle #" + nextID, route);
    //shuttleFeet.add(shuttle);
    fleet.addVehicle(shuttle);
    nextID++;
}

编辑:以下是提到的其他三个类:

车辆类:

public class Vehicle
{
// A unique ID for taxi/shuttle.
protected String id;
//private String id;
// The next destination of this taxi/shuttle.
//private String destination;
protected String destination;
// The location of this taxi/shuttle.
//private String location;
protected String location;
//The fleet of taxis and shuttles.
//protected ArrayList<Vehicles> vehicles;

/**
 * Constructor for objects of class Vehicle
 */
public Vehicle(String destination, String base, String id)
{
    // initialise instance variables.
    //this();
    this.id = id;
    //this.destination = destination;
    location = base;
    destination = null;
}

 /**
 * Return the ID of the taxi/shuttle.
 * @return The ID of the taxi/shuttle.
 */
public String getID()
{
    //return this.id;
    return id;
}

/**
 * Accessor method for the taxi/shuttle ID.
 */
protected void setID(String id) 
{
    this.id = id;
    //return id;
}

/**
 * Return the destination of the taxi/shuttle.
 * @return The destination of the taxi/shuttle.
 */
public String getDestination()
{
    return destination;
}

/**
 * Set the intended destination of the taxi/shuttle.
 * @param destination The intended destination.
 */
public void setDestination(String destination)
{
    this.destination = destination;
}

/**
 * Return the location of the taxi/shuttle.
 * @return The location of the taxi/shuttle.
 */
public String getLocation()
{
    return location;
}

/**
 * Set the intended location of the taxi/shuttle.
 * @parm location The intended location.
 */
public void setLocation(String location)
{
    this.location = location;
}

/**
 * Return the status of this taxi/shuttle.
 * @return The status.

public String getStatus()
{
    return id + " at " + location + " headed for " +
           destination;
}
*/

出租车类:

public class Taxi extends Vehicle
{
//Moved to the Vehicle SuperClass.
//private String id;
// The destination of this taxi.
//private String destination;
// The location of this taxi.
//private String location;
// Whether it is free or not.
private boolean free;

/**
 * Constructor for objects of class Taxi.
 * @param base The name of the company's base.
 * @param id This taxi's unique id.
 */
public Taxi(String destination, String id, String base)
{
    //this.id = id;
    //super();
    super(destination, id, base);
    //location = base;
    //super(location);
    //destination = null;
    free = true;
}

/**
 * Book this taxi to the given destination.
 * The status of the taxi will no longer be free.
 * @param destination The taxi's destination.
 */
public void book(String destination)
{
    setDestination(destination);
    free = false;
}

/**
 * Return the status of this taxi.
 * @return The status.
*/
public String getStatus()
{
    return id + " at " + location + " headed for " +
           destination;
}


/**
 * Return the ID of the taxi.
 * @return The ID of the taxi.

public String getID()
{
    //return id;
}
*/

/**
 * Accessor method for the taxi/shuttle ID.
 */
public void setID(String id)
{
    super.setID(id);
    //return id;
}

/**
 * Return the location of the taxi.
 * @return The location of the taxi.

public String getLocation()
{
    return location;
}
*/

/**
 * Return the destination of the taxi.
 * @return The destination of the taxi.

public String getDestination()
{
    return destination;
}
*/

/**
 * Set the intented destination of the taxi.
 * @param destination The intended destination.
 */
public void setDestination(String destination)
{
    super.setDestination(destination);
    //return destination;
}

/**
 * Set the intented destination of the taxi.
 * @param destination The intended destination.
 */
public void setLocation(String location)
{
    super.setLocation(location);
    //return location;
}

/**
 * Indicate that this taxi has arrived at its destination.
 * As a result, it will be free.
 */
public void arrived()
{
    location = destination;
    destination = null;
    free = true;
}
}

班车课程:

public class Shuttle extends Vehicle
{
//Moved to the Vehicle SuperClass.
//private String id;
// The next destination of this shuttle on its
// circular route.
//private String destination;
// The location of this shuttle.
//private String location;
// The circular route of this shuttle.
private ArrayList<String> route;
// The destination number in route that the shuttle is
// currently headed for.
private int destinationNumber;

/**
 * Constructor for objects of class Shuttle
 * @param id This shuttle's unique id.
 * @param route The route taken by this shuttle.
 *              The first entry is the starting location.
 */
public Shuttle(String destination, String id, String base, ArrayList<String> route)
{
    //this.id = id;
    super(id, destination, base);
    setRoute(route);
}

/**
 * Return the status of this shuttle.
 * @return The status.
 */ 
public String getStatus()
{
    return id + " at " + location + " headed for " +
           destination;
}


/**
 * Return the ID of the shuttle.
 * @return The ID of the shuttle.

public String getID()
{
    return id;
}
*/

/**
 * Accessor method for the taxi/shuttle ID.
 */
public void setID(String id)
{
    super.setID(id);
    //return id;
}

/**
 * Return the location of the shuttle.
 * @return The location of the shuttle.

public String getLocation()
{
    return location;
}
*/

/**
 * Return the destination of the shuttle.
 * @return The destination of the shuttle.

public String getDestination()
{
    return destination;
}
*/

/**
 * Indicate that this shuttle has arrived at its next destination.
 */
public void arrived()
{
    location = destination;
    setNextDestination();
}

/**
 * Set the next destination of the shuttle.
 */
private void setNextDestination()
{
    destinationNumber++;
    if(destinationNumber >= route.size()) {
        // End of the circular route.
        // Start from the beginning again.
        destinationNumber = 0;
    }
    setDestination(route.get(destinationNumber));
}

/**
 * Set the intented destination of the suttle.
 * @param destination The intended destination.
 */
public void setDestination(String destination)
{
    //this.destination = destination;
    super.setDestination(destination);
    //return destination;
}

 /**
 * Set the intended location of the taxi/shuttle.
 * @parm location The intended location.
 */
public void setLocation(String location)
{
    this.location = location;
}

/**
 * Set the route for this shuttle.
 * @param route The circular list of destinations.
 */
private void setRoute(ArrayList<String> route)
{
    // Make a copy of the list parameter.
    this.route = new ArrayList<String>();
    this.route.addAll(route);
    destinationNumber = 0;
    location = route.get(destinationNumber);
    setNextDestination();
}

}

2 个答案:

答案 0 :(得分:0)

对于一件简单的事情这么长的问题。这一行:

new Vehicle(base, "Car #" + nextID)

正如错误消息所述,您的Vehicle构造函数需要3个字符串,并且您只需在此处提供2个字符串。将其更改为:

new Vehicle(base, "Car #" + nextID, "some string?");

它会起作用。当然要检查最后一个参数需要什么,我在这里看不到它会导致这段代码没有包含在内。

答案 1 :(得分:0)

为什么Taxi中的Vehicle中的方法与Vehicle中的方法相同,是不是可以通过String访问它们的继承点?你的代码到底工作了吗?