2013-05-07 36 views
-1

我在使用Inheritance構造類,並且在使用super()時遇到了困難。我有InterestCalculator class這是CompoundInterest Class的母公司。我想在複利class.Your構造使用super() InterestCalculator需要3 paramenters(本金,的InterestRate和長期) 我需要調用CompoundInterest這需要4 parameters,我需要通過3( PrincipalAmount,InterestRate和Term)4th parameter特定於CompoundInterestjava在繼承中使用super()

import java.util.Scanner; 
public class InterestCalculator 
{ 

    protected double principalAmount; 
    protected double interestRate; 
    protected int term; 

    public InterestCalculator(double principalAmount, double interestRate,   int term) 
    { 
    this.principalAmount = principalAmount; 
    this.interestRate= interestRate; 
    this.term = term; 
    } 
    public double getPrincipal() 
    { 
     return principalAmount; 
    }  
    public double getInterestRate() 
    { 
    return interestRate; 
    } 
    public double getTerm() 
    { 
    return term; 
    } 
    public double calcInterest() 
    { 
    return -1; 
    } 
    protected double convertTermToYears() 
    { 
    return (this.term/12.0); 
    } 
    protected double convertInterestRate() 
    { 
    return (this.interestRate/100.0); 
    } 
} 
public class CompoundInterest extends InterestCalculator 
{ 
    protected int compoundMultiplier = 0;  

    super.CompoundInterest(double compoundMultiplier); 
    { 
    super.principalAmount = principalAmount; 
    super.interestRate = interestRate; 
    super.term = term; 
    this.compoundMultiplier = (int) compoundMultiplier; 
    } 
    public double calcInterest() 
    { 
    double calcInterest = (super.principalAmount *Math.pow((1.0+((super.convertInterestRate())/this.compoundMultiplier)),(this.compoundMultiplier *(super.convertTermToYears()))))- super.principalAmount;    
    return calcInterest;  
    } 
} 
+0

你只是補差。爲什麼不學習語言? – EJP 2013-05-07 03:29:17

+0

看來你正在編寫一個不知道java的構造函數。請閱讀http://docs.oracle.com/javase/tutorial/java/IandI/super.html – Drogba 2013-05-07 03:30:06

回答

1

您需要定義在派生類的構造函數接受所有它要需要的參數:

public class CompoundInterest extends InterestCalculator { 
    protected int compoundMultiplier; 

    /** 
    * Constructor for CompoundInterest. 
    */ 
    public CompoundInterest(double principalAmount, double interestRate, int term, 
     int compoundMultiplier) 
    { 
     super(principalAmount, interestRate, term); 
     this.compoundMultiplier = compoundMultiplier; 
    } 

    . . . // rest of class 
} 

注意,在構造函數中,super(...)調用父類的構造。您不要通過將super.放在您的構造函數名稱前面來執行此操作。 通過調用super(...)(如果存在,它必須是子類構造函數的第一行),將調用匹配的基類構造函數,並將使用參數來設置適當的字段。沒有必要嘗試從子類中再次嘗試。

0

CompoundInterest類有幾個錯誤。

1 - 您必須調用superclasst構造函數。由於InterestCalculator只有一個構造函數(double, double, int),因此您需要在CompoundInterest構造函數的開始處對其進行調用。

2 - 您在super.CompoundInterest上以錯誤的方式使用supersuper用於訪問超類中的方法。您必須將其重命名爲CompoundInterest

3 - 如第一項所述,您需要調用超類的構造函數。你這樣做:super(principalAmount, interestRate);。記住:它必須是構造函數的第一個調用。

4 - 您未傳遞給CompountInterest它所期望的三個參數中的任何一個。您應該考慮將這些參數也放入CompoundInterest中。

如果您將這些改變,你的代碼將魔神像這樣:

CompoundInterest(double compoundMultiplier, double principalAmount, double interestRate, int term); { 
    super(principalAmount, interestRate, term); 
    this.compoundMultiplier = (int) compoundMultiplier; 
}