2013-05-07 64 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; 
}