这里总新手(感谢轴承)。我创建了一个抽象的父类:
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。),剩下的只是200美元的存款,加上1000美元的初始,NewBal的10美元利息。
答案 0 :(得分:0)
有些事情对我没有任何意义。在父类中,计算年度收入的方法是采用名为interest
的变量,但随后将其完全丢弃,而使用局部变量interestRate
。
public double annualEarnings (double interest) {
return this.interest = 1000 * setInterestRate(interestRate);
}
我认为应该是
public double annualEarnings (double interest) {
return this.interest = 1000 * setInterestRate(interest);
}