【发布时间】:2014-05-21 05:50:26
【问题描述】:
从事 Junit 测试,遇到问题。
我有一个车辆超类,以及摩托车和汽车子类。我需要使用超类中的方法来检索子类中的信息(字符串ID,int到达)
public class MotorCycle extends Vehicle {
public String bikeID;
public int arrivalTimeA;
public MotorCycle(String vehID, int arrivalTime) throws VehicleException {
super(bikeID, arrivalTimeA);
bikeID = vehID;
arrivalTimeA = arrivalTime;
我正在使用哪个子类,我想使用超类车辆中的方法从摩托车中检索信息
public Vehicle(String vehID,int arrivalTime) throws VehicleException {
vehicleID = vehID;
arrivalTimeA = arrivalTime;
if (arrivalTime <=0){
throw new VehicleException("Vehicle");
}
public String getVehID() {
return vehicleID;
}
尝试在子类中使用 super(vehID,arrivalTime),但我必须将变量设为静态,这对这个程序没有好处。
每当我添加 super(vehID,arrivalTime) 时,我都会不断收到错误提示“将变量设为静态”,如果我不这样做,它只是无法正常运行并失败。
我的测试是:
@Test
public void testGetVehID() throws VehicleException {
moto = new MotorCycle("b1234", 600);
veh = moto;
String id = veh.getVehID();
assertEquals("b1234", id);// TODO
}
veh 只是一个空的 Vehicle 类对象。
我在访问信息时遇到问题。每次我运行这个测试或类似的东西时,int 的值为 0,String 的值为 null
【问题讨论】:
-
为什么需要为 super(vehID,arrivalTime) 设置静态变量?
-
你的问题是否与this有关
-
代码没问题,有什么问题?
-
当我尝试测试它时,摩托车没有将值传递给车辆
标签: java inheritance junit subclass superclass