在调用超类型构造函数之前无法引用 "X" 错误消息
Cannot Reference "X" before supertype constructor has been called error message
我目前正在尝试将 2 个子 class 重构为一个超级 class。 sub classes 称为 Taxi
和 Shuttle
,superclass 称为 Vehicle
。我已经移动了两者中的重复代码,并使 subclasses 中的构造函数调用 super 方法,如下所示,但我不断收到错误消息
Cannot Reference "ident" before supertype constructor has been called)
为什么?
这里是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)
构造函数,只有 public Vehicle(String id, String base)
构造函数 .
Taxi
的构造函数应该调用正确的(现有的)超级构造函数。
注意,我们会将局部变量id
传递给超级构造函数,传递给Vehicle
它自己的空ident
变量是没有意义的。
public Taxi(String id, String base)
{
super(id, base);
free = true;
}
Shuttle
的构造函数缺少一个 id
参数(来自其文档),让我们添加它并使用列表的第一个条目作为 base
参数(再次来自它的文档):
/**
* 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 id, ArrayList<String> route)
{
super(id,route.get(0));
setRoute(route);
}
我目前正在尝试将 2 个子 class 重构为一个超级 class。 sub classes 称为 Taxi
和 Shuttle
,superclass 称为 Vehicle
。我已经移动了两者中的重复代码,并使 subclasses 中的构造函数调用 super 方法,如下所示,但我不断收到错误消息
Cannot Reference "ident" before supertype constructor has been called)
为什么?
这里是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)
构造函数,只有 public Vehicle(String id, String base)
构造函数 .
Taxi
的构造函数应该调用正确的(现有的)超级构造函数。
注意,我们会将局部变量id
传递给超级构造函数,传递给Vehicle
它自己的空ident
变量是没有意义的。
public Taxi(String id, String base)
{
super(id, base);
free = true;
}
Shuttle
的构造函数缺少一个 id
参数(来自其文档),让我们添加它并使用列表的第一个条目作为 base
参数(再次来自它的文档):
/**
* 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 id, ArrayList<String> route)
{
super(id,route.get(0));
setRoute(route);
}