2013-04-21 78 views
-4

在程序開始時我有class BusinessAccount extends GasAccountGasAccount是超類,BusinessAccount是子類。我已經做了超類的子類,但它不承認「擴展」

我也有問題的底部重載方法,這意味着重寫超類方法來計算對象的折扣對象的折扣適用於新單位的成本的對象的餘額將 添加到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 

任何幫助將是偉大的!

+0

後整個代碼,請!沒有任何東西可以用這些微不足道的信息來推斷! – NINCOMPOOP 2013-04-21 17:17:12

+4

'public double recordUnits'如果你說它返回'double',則返回'double';如果你想返回一個'String',就這麼說。 – 2013-04-21 17:17:24

+0

根據您的描述,聽起來像您的第一個問題是您的BusinessAccount類不公開。類聲明看起來應該像'public class BusinessAccount extends GasAccount'。 – Laf 2013-04-21 17:18:52

回答

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。

總是閱讀並嘗試理解編譯器的錯誤消息。如果你不理解他們,請將他們發佈在你的問題中。錯誤消息旨在被讀取,幷包含有意義的信息。

3

公共雙recordUnits(雙dblUnitsUsed)

這種方法已經被定義爲返回雙重但是你return語句是字符串。如果您想返回字符串,你應該使用:

公共字符串recordUnits(雙dblUnitsUsed){}

相關問題