子类方法未正确减去

问题描述 投票:-1回答:1

我的任务:创建一个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();

    }
}
java superclass
1个回答
2
投票

[您要提取的金额比金额少2 –假设学生要提取10,他们得到10,但余额减少8。您给学生的功劳 2。

您可能是说

public void withdrawal(double amount){
    super.withdrawal(amount + 2);   
}
© www.soinside.com 2019 - 2024. All rights reserved.