子类方法不能正确减去
Subclass method doesn't subtract correctly
我的任务:创建一个 Account super class 和一个 StudentAccount subclass。 StudentAccount 的不同之处在于,他们存款可获得 1 美元的奖金,但提款则需支付 2 美元的费用。我为 subclass 中的方法覆盖了 superclass 方法。唯一似乎不起作用的方法是我的提款方法。
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 bonus on depositing
@Override
public void deposit(double amount){
super.deposit(amount + 1);
}
// Students pay a 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。您给学生一个 credit 每次提款 2。
你的意思可能是
public void withdrawal(double amount){
super.withdrawal(amount + 2);
}
我的任务:创建一个 Account super class 和一个 StudentAccount subclass。 StudentAccount 的不同之处在于,他们存款可获得 1 美元的奖金,但提款则需支付 2 美元的费用。我为 subclass 中的方法覆盖了 superclass 方法。唯一似乎不起作用的方法是我的提款方法。
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 bonus on depositing
@Override
public void deposit(double amount){
super.deposit(amount + 1);
}
// Students pay a 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。您给学生一个 credit 每次提款 2。
你的意思可能是
public void withdrawal(double amount){
super.withdrawal(amount + 2);
}