我目前正試圖將2個子類重構爲超類。子類別稱爲Taxi
和Shuttle
,超類別稱爲Vehicle
。我提出兩個重複的代碼,並提出在子類的構造函數調用的方法超級如下圖所示,但我不斷收到錯誤消息超類型構造函數被調用之前不能引用「X」錯誤消息
不能引用「標識」超構造函數被調用之前)
爲什麼?
這裏的Vehicle
public class Vehicle
{
// instance variables - replace the example below with your own
protected String ident;
// The Destination Of Taxi and Shuttle
protected String destination;
// The location of this shuttle.
protected String location;
/**
* Constructor for objects of class Vehicle
*/
public Vehicle(String id, String base)
{
// initialise instance variables
this.ident = id;
location = base;
destination = null;
}
/**
* 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;
}
/**
* Return the ID of the shuttle.
* @return The ID of the shuttle.
*/
public String getIdent()
{
return ident;
}
}
這裏的Taxi
public class Taxi extends Vehicle
{
// 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 id, String base)
{
super(ident);
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 vehicle.ident + " at " + location + " headed for " +
destination;
}
/**
* Indicate that this taxi has arrived at its destination.
* As a result, it will be free.
*/
public void signalArrival()
{
location = destination;
destination = null;
free = true;
}
}
這裏的Shuttle
import java.util.ArrayList;
/**
* A shuttle.
* Shuttles have a unique ID, a location and a list of destinations.
* They operate a circular route.
*
* @author David J. Barnes
* @version 2016.12.04
*/
public class Shuttle extends Vehicle
{
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(ArrayList<String> route)
{
super(ident);
setRoute(route);
}
/**
* Return the status of this shuttle.
* @return The status.
*/
public String getStatus()
{
return ident + " at " + location + " headed for " +
destination;
}
/**
* Indicate that this shuttle has arrived at its next destination.
*/
public void signalArrival()
{
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 route for this shuttle.
* @param route The circular list of destinations.
*/
private void setRoute(ArrayList<String> route)
{
if(route.size() < 2) {
throw new IllegalStateException("setRoute must have at least two destinations");
}
// Make a copy of the list parameter.
this.route = new ArrayList<String>();
this.route.addAll(route);
destinationNumber = 0;
location = route.get(destinationNumber);
setNextDestination();
}
}
'Vehicle'沒有'Public Vehicle(String id)'構造函數。另外,通過傳遞給'Vehicle'它自己的'ident'變量,它從未初始化,您期望什麼? – Berger
可以請你發表一個例子,請我理解但是我失去了。 –