我目前正在制作一个存储拖拉机租赁数据的程序,我在拖拉机类遇到问题,我需要覆盖toString方法打印出所有的信息。拖拉机,也是一种计算租金利润的方法,这只是出租天数*出租率,但我收到这些错误,我不知道从哪里开始。
public int RentalProfit(int RentalRate, int RentalDays)
{
RentalProfit = RentalRate * RentalDays;
return this.RentalProfit;
}
@Override
public String toString() {
return "Tractor (Rental days = " + RentalDays + ", Rental Rate = " + RentalRate +
"Rental profit = " RentalProfit + ", VehicleID = " + VehicleID ")";
}
继承错误:
Tractor.java:59: error: ';' expected
"Rental profit = " RentalProfit + ", VehicleID = " + VehicleID ")";
^
Tractor.java:59: error: not a statement
"Rental profit = " RentalProfit + ", VehicleID = " + VehicleID ")";
^
Tractor.java:59: error: ';' expected
"Rental profit = " RentalProfit + ", VehicleID = " + VehicleID ")";
^
3 errors
public static void main(String[] args){
Tractor tractor;
tractor = new Tractor();
tractor.setRentalRate(9);
tractor.setRentalDays(45);
tractor.setVehicleID(9145949);
toString();
继承人错误:
testTractor.java:11: error: non-static method toString() cannot be referenced from a static context
toString();
^
1 error
答案 0 :(得分:1)
您错过了+
标志:
return "Tractor (Rental days = " + RentalDays + ", Rental Rate = " + RentalRate +
"Rental profit = " + RentalProfit + ", VehicleID = " + VehicleID + ")";
^ ^
here and here
答案 1 :(得分:0)
在你的main方法中,你调用toString();
你不能这样做 - toString不是静态方法,必须通过实例调用。你想要的是tractor.toString();
答案 2 :(得分:0)
第一个错误你返回字符串你必须像这样关闭它
return ("Tractor Rental days = " + RentalDays + " Rental Rate = " + RentalRate +
"Rental profit = "+ RentalProfit + ", VehicleID = " + VehicleID );
在第二个问题 我认为你有main方法而且它是静态的你需要使用静态方法来调用它使方法toString()静态... 我希望我帮助你