我从超类中创建了一个子类,但它没有认识到“扩展”

时间:2013-04-21 17:15:25

标签: java netbeans

在程序开始时我得到了class BusinessAccount extends GasAccountGasAccount是超类,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 

任何帮助都会很棒!

2 个答案:

答案 0 :(得分:3)

public double recordUnits(double dblUnitsUsed)

此方法已定义为返回double,但返回语句是字符串。如果你想返回字符串,你应该使用:

public String recordUnits(double dblUnitsUsed){}

答案 1 :(得分: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";
}

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

当然,正如注释中所指出的,返回double的方法不能返回String。

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