-4

在程序开始时,我得到了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 

任何帮助都会很棒!

4

2 回答 2

3

公共双记录单位(双 dblUnitsUsed)

此方法已定义为返回一个双精度值,但是您的返回语句是字符串。如果您想返回字符串,您应该使用:

公共字符串记录单元(双 dblUnitsUsed){}

于 2013-04-21T17:24:15.410 回答
1

您发布的代码无效:方法声明在右括号和左大括号之间不能有分号:

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";
}

还要学习像我上面所做的那样缩进你的代码以使其可读。

当然,正如评论中所指出的,返回双精度的方法不能返回字符串。

始终阅读并尝试理解编译器的错误消息。如果您不理解它们,请将它们发布在您的问题中。错误消息旨在被阅读,并包含有意义的信息。

于 2013-04-21T17:20:04.707 回答