我目前正在尝试将两个子类重构成一个超类。子类称为 Taxi
以及 Shuttle
这个超类叫做 Vehicle
. 我已经移动了这两个类中的重复代码,并使子类中的构造函数调用方法super,如下所示,但我一直收到错误消息
在调用父类型构造函数之前不能引用“ident”)
为什么?
这里是 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 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 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 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();
this.route.addAll(route);
destinationNumber = 0;
location = route.get(destinationNumber);
setNextDestination();
}
}
1条答案
按热度按时间iezvtpos1#
Vehicle
没有一个public Vehicle(String id)
构造函数,只有一个public Vehicle(String id, String base)
建造师。建造商
Taxi
应该调用正确的(现有的)超级构造函数。注意,我们将传递局部变量
id
传给超级建造师,传给Vehicle
它自己是空的ident
变量是没有意义的。建造商
Shuttle
缺少一个id
参数,让我们添加它并使用列表的第一个条目作为base
参数(同样来自其文档):