从派生类调用Overridden方法

时间:2015-11-25 11:29:14

标签: java

我有两个派生自存储在ArrayList中的Account类的对象。我试图用一个基本的for循环调用重写的方法,但是我不能让它一致地工作。

import java.util.ArrayList;
import java.util.Scanner;

public class InheritanceTest 
{

private static class Account
{

    //Constructor
    Account()
    {

    }

    private void processDeposit()
    {


        double deposit = 0;

        System.out.println("Please enter the deposit amount: ");
        deposit = input.nextDouble();

        balance += deposit;     
    }

    private void processWithdrawal()
    {
        double withdrawal = 0;

        System.out.println("Please enter the amount to be drawn from the account: ");
        withdrawal = input.nextDouble();
        balance -= withdrawal;

    }

    public String getAccountName() {
        System.out.println("Creating " + "account, enter name: ");
        accountName = input.nextLine();
        return accountName;
    }

    public void setAccountName(String accountName) {
        this.accountName = accountName;
    }

    public int getAcctID() {
        System.out.println("Your " + "account number is " + acctID);
        return acctID;
    }

    public void setAcctID(int acctID) {
        this.acctID = acctID;
        acctID++;
    }

}

//

 private static class CheckingAccount extends Account
{

    public String getAccountName() {
        System.out.println("Creating checking account, enter name: ");
        accountName = input.nextLine();
        return accountName;
    }

    public void setAccountName(String accountName) {
        this.accountName = accountName;
    }

    private void processWithdrawal()
    {
        double withdrawal = 0;

        System.out.println("Please enter the amount to be drawn from the account: ");
        withdrawal = input.nextDouble();

        if (withdrawal > balance)
        {
            balance -= 10;
            System.out.println("Overdraft fee has been charged");
        } else
            balance -= withdrawal;

    }

}

//

 private static class SavingsAccount extends Account
{

    public String getAccountName() {
        System.out.println("Creating savings account, enter name: ");
        accountName = input.nextLine();
        return accountName;
    }

    public void setAccountName(String accountName) {
        this.accountName = accountName;
    }

    private void processWithdrawal()
    {
        double withdrawal = 0;

        System.out.println("Please enter the amount to be drawn from the account: ");
        withdrawal = input.nextDouble();
        if(withdrawal > balance)
        {
            System.out.println("Insufficient funds");
        } else
        {
            balance -= withdrawal;
        }

    }


    }

//

public static void main(String[] args)
{
    ArrayList<Account> Bank = new ArrayList<Account>();
    CheckingAccount checking1 = new CheckingAccount();
    Bank.add(checking1);
    SavingsAccount savings = new SavingsAccount();
    Bank.add(savings);

    for(int i = 0; i<Bank.size(); i++)
    {
        Bank.get(i).getAccountName();
        Bank.get(i).setAccountName(Bank.get(i).accountName);
        Bank.get(i).getAcctID();
        Bank.get(i).setAcctID(Bank.get(i).acctID);
    }

    for (int i =0; i<Bank.size(); i++)
    {
        System.out.println("Processing Account: " + Bank.get(i).acctID);
        Bank.get(i).processDeposit();
        System.out.println("Balance: " + Bank.get(i).balance);
        Bank.get(i).processWithdrawal();
        System.out.println("Balance: " + Bank.get(i).balance);
        Bank.get(i).processWithdrawal();
        Bank.get(i).displayAccount(Bank.get(i).acctID, Bank.get(i).accountName, Bank.get(i).balance);

    }
}

}

第一个循环调用重写的getAccountName方法就好了,但第二个循环似乎只是从基类调用processWithdrawal方法。任何人都可以指出我找出为什么会发生这种情况的方向吗?

2 个答案:

答案 0 :(得分:1)

 private void processWithdrawal()

公开

 public void processWithdrawal()

答案 1 :(得分:0)

当您想要覆盖方法,即利用动态多态性的特征时,该方法应该受到保护/公开。

私有方法仅限于声明它的特定类,它不会使Overriding成为可能。