割り当てのために、多形性と継承を既存のプログラムに導入しようとしています。多態性の導入:2つの配列リストを1つの配列リスト(Java)に置換する
私が現在抱えている問題は、2つの別々のリストを1つのリストに置き換えることです。私は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<Vehicle>
でArrayList<Shuttle> shuttleFleet
とArrayList<Taxi> taxiFleet
を交換する必要があります。私はもともと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++;
}
EDIT:
Vehicleクラス:
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();
}
ここで言及された他の3つのクラスがあります
}
クラス定義を複数のコードブロックに分割することなく表示できますか?少なくともVehicle、Taxi、Shuttleの定義は表示されていません。 – birryree
あなたに 'Vehicle'クラスを見せてください。 3つのパラメータを持つコンストラクタがあり、2つしか渡していないようです。 – talex
あなたが言及している問題のあるコード 'new Vehicle(base、" Car# "+ nextID);はあなたが表示するコードのどこにも存在しません。 – sim642