【发布时间】:2020-03-24 17:06:02
【问题描述】:
我的任务:创建一个 Account 超类和一个 StudentAccount 子类。 StudentAccount 的不同之处在于存款可获得 1 美元的奖金,而提款则需支付 2 美元的费用。我为子类中的方法覆盖了超类方法。唯一似乎不起作用的方法是我的退出方法。
public class BankTester
{
public static void main(String[] args)
{
Account deez = new Account("Bob", 10.0);
Account jeez = new StudentAccount("Bobby", 10.0);
jeez.withdrawal(2.0);
System.out.println(jeez);
deez.withdrawal(2.0);
System.out.println(deez);
}
}
public class Account
{
private String name;
private double balance;
// Initialize values in constructor
public Account(String clientName, double openingBal){
name = clientName;
balance = openingBal;
}
// Complete the accessor method
public double getBalance(){
return balance;
}
// Add amount to balance
public void deposit(double amount){
balance += amount;
}
// Subtract amount from balance
public void withdrawal(double amount){
balance -= amount;
}
// Should read: Regular account with a balance of $__.__
public String toString(){
return "Regular account with a balance of $" + balance;
}
}
public class StudentAccount extends Account
{
// Complete this class with Override methods.
public StudentAccount(String studentName, double
openingBal){
super(studentName, openingBal);
}
// Students get a $1 bonus on depositing
@Override
public void deposit(double amount){
super.deposit(amount + 1);
}
// Students pay a $2 fee for withdrawing
@Override
public void withdrawal(double amount){
super.withdrawal(amount - 2);
}
// toString() Should read: Student account with a
balance of $__.__
@Override
public String toString(){
return "Student account with a balance of $" +
super.getBalance();
}
}
【问题讨论】:
-
是时候学习如何调试了。 似乎不起作用 不是错误描述。你能解释一下发生了什么吗?
-
什么不起作用?
-
withdrawal(amount - 2)的意思是,当您的学生想要提取 10 美元时,银行会给他 8 美元。 -
supper.withdrawal 不影响学生余额。相反,学生应该有自己的平衡持有人,您将在构造函数中设置。说 studentSalance,然后执行您在 Account 中所做的操作。 this.studentBalance -= 金额;
标签: java superclass