【问题标题】:Child Class argument not being accepted by Super Abstract Class Parent超抽象类父级不接受子类参数
【发布时间】:2017-06-22 06:27:09
【问题描述】:

这里是新手(感谢您的耐心)。我创建了一个抽象父类:

public abstract class Account {


public double setInterestRate (double interestRate) {

    return this. interestRate = interestRate / 100;


public abstract void withdraw(double amount);


public void deposit(double amount) {
    if (amount < 0) { // amount is invalid: exception occurs
        throw new IllegalArgumentException("Error: Deposit cannot be a"
            + " negative value.");
    } else {
    this.deposit = amount;  
    balance = balance + amount;

    }
}

public void calculateBalance () {

        double interestTotal = balance * setInterestRate(interestRate);
        this.balance = balance + interestTotal;

}    
public double annualEarnings (double interest) {

  return this.interest =  1000 * setInterestRate(interestRate);

}

在子类中,我想将 AnnualInterest 变量作为参数传递给我在 toString() 中调用的 super.annualEarnings(annualInterest) 方法:

public class CheckingAccount extends Account implements Overdraftable {

protected double annualInterest = 1.0;

@Override
public String toString() {

    return super.toString() + "\n"+ account + "\t\t" + deposit + "\t\t" 
        + withdrawAmt +"\t\t" + super.annualEarnings(annualInterest)+ "\t\t" + super.balance ;

}

我希望得到这种类型的打印输出:

=======================================
AccNo. Deposit  Withdraw Intr.  NewBal
=======================================
101    200.00   0.00    10.00  1210.00

每年 1000 美元的初始单利为 10 美元,即 10.00 美元(Intr.),其余的只是在 1000 美元的初始存款中加上 200 美元的押金,而 NewBal 的利息为 10 美元。

【问题讨论】:

  • 你能提一下,你看到的实际输出/错误是什么,类的完整结构也会有所帮助
  • @AkashYadav 刚刚添加了全部内容 - 谢谢伙计!
  • 仍然需要很多东西才能让它工作。请看看这个en.wikipedia.org/wiki/Minimal_Working_Example
  • @AmrElAdawy 我很欣赏你的洞察力,但不需要对我消极攻击——我是来学习的。最初它是一个 MWE,但您会注意到上面的评论。
  • 当然,我并不想以任何方式咄咄逼人。我试图给你一些提示,以免被否决。我已经尝试了上面的代码,但是缺少部分有很多错误。作为一个很好的提示,尝试将该代码放入一个新项目中并查看它的外观。我正在努力提供帮助。但你也必须支持。祝你好运

标签: java inheritance methods arguments


【解决方案1】:

有些事情对我来说没有任何意义。在父类中,计算年收入的方法是采用一个名为interest 的变量,但随后它被完全丢弃,而是使用局部变量interestRate

public double annualEarnings (double interest) {

  return this.interest =  1000 * setInterestRate(interestRate);
}

我觉得应该是

    public double annualEarnings (double interest) {

      return this.interest =  1000 * setInterestRate(interest);
    }

【讨论】:

  • 感谢您的来信。我试过了,它改变了一切——现在运行良好。正如他们所说,细节中的魔鬼。感谢您的帮助!
  • 很高兴它成功了。请随时提出任何要求。
猜你喜欢
  • 1970-01-01
  • 2013-12-03
  • 1970-01-01
  • 2021-10-03
  • 1970-01-01
  • 2012-02-05
  • 2012-03-08
  • 1970-01-01
相关资源
最近更新 更多