Java 子类方法不';不能正确地减去
我的任务:创建Account超类和StudentAccount子类。学生账户的不同之处在于,他们的存款可获得1美元的奖金,取款可获得2美元的费用。我重写了子类中方法的超类方法。唯一似乎不起作用的方法是我的退出法Java 子类方法不';不能正确地减去,java,superclass,Java,Superclass,我的任务:创建Account超类和StudentAccount子类。学生账户的不同之处在于,他们的存款可获得1美元的奖金,取款可获得2美元的费用。我重写了子类中方法的超类方法。唯一似乎不起作用的方法是我的退出法 public class BankTester { public static void main(String[] args) { Account deez = new Account("Bob", 10.0); Account jeez
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();
}
}
你提取的金额比金额少了2个-假设学生想提取10个,他们得到10个,但他们的余额减少了8个。每次取款你都给学生2分 你可能是说
public void withdrawal(double amount){
super.withdrawal(amount + 2);
}
是时候学习如何调试了。似乎不起作用不是错误描述。你能解释一下发生了什么吗?什么不起作用?
取款(金额-2)
的意思是,当你的学生想取款10美元时,银行会给他8美元。取款不会影响学生的余额。相反,学生应该有自己的余额持有人,您将在构造函数中设置余额持有人。说学生薪水然后做你在帐户上做的事。此项。学生余额-=金额;