在程序开始时,我有class BusinessAccount extends GasAccount
。GasAccount
为超类,BusinessAccount
为子类。
我也有重载方法在底部的问题,这意味着重写超类方法来计算GasAccount
的平衡对象与对象的折扣应用到新单位的成本,这反过来将是添加到BusinessAccount
对象的当前余额。这个方法将返回一个String
值指示事务成功或不成功。对于这个方法
import java.util.Scanner ;
class BusinessAccount extends GasAccount
{
// The objects properties are declared as private
private double dblDiscount ;
// The objects methods are defined as public
public BusinessAccount (int intNewAccRefNo, String strNewName, String
strNewAddress,double dblNewUnits, double dblNewDiscount, int intAccRefNo, String strName, double dblUnits, String strAddress )
{
// The constructor - same name as the class and no return type
dblDiscount = dblNewDiscount;
}
public void setNewDiscount ( double dblNewDiscount )
{
dblDiscount = dblNewDiscount ;
} // end of setNewDiscount
public double getDiscount()
{
return dblDiscount ;
} // end of getDiscount method
public string recordUnits ( double dblUnitsUsed);
if ( dblDiscount = 0.00){
dblBalance = (dblUnitCost * dblUnitsUsed) + dblBalance;
return "Successful";
}
else {
dblBalance = ((dblUnitCost * dblUnitUSed) 100 * dblDiscount) + dblBalance;
return "Unseccfessful";
} // end of Override method
} // end of class
任何帮助将是伟大的!
public double recordUnits (double dblUnitsUsed)
这个方法被定义为返回double类型,但是你的返回语句是字符串。如果你想返回字符串,你应该使用:
public String recordUnits (double dblUnitsUsed){}
您发布的代码无效:方法声明不得在右括号和左括号之间有分号:
public double recordUnits(double dblUnitsUsed) // no semicolon here
{
同样适用于if和else语句,如果它们包含多条指令,则必须将它们用大括号括起来:
if (dblDiscount = 0.00) {
dblBalance = (dblUnitCost * dblUnitsUsed) + dblBalance;
return "Successful";
}
else {
dblBalance = ((dblUnitCost * dblUnitUSed) 100 * dblDiscount) + dblBalance;
return "Unsuccfessful";
}
也要学习像我上面所做的那样缩进你的代码,使其可读性。
当然,如注释所示,返回double类型的方法不能返回String类型。
总是阅读并尝试理解编译器的错误消息。如果你不明白,把它们贴在你的问题里。错误消息是用来读取的,并且包含有意义的信息。