C#在对象数组上使用子类方法

时间:2018-02-19 19:09:11

标签: c#

我确定我错过了一些小事,但我不能为我的生活弄清楚。我正在尝试使用子类方法CalculateInterest用于子类SavingsAccount,子类属性TransactionFee用于下面脚本中的子类CheckingAccount。基本上,测试程序应循环遍历数组中的每个对象,并根据对象是CheckingAccount还是SavingsAccount状态获得的利息或交易费用。

我得到的错误是在Account类中没有定义CalculateInterest()和TransactionFee。

using System;

class BankAccountTester {

    static void Main() {

    Account[] accounts = new Account[4];
    accounts[0] = new SavingsAccount(25, 3);    
    accounts[1] = new CheckingAccount(80, 1);
    accounts[2] = new SavingsAccount(200, 1.5);
    accounts[3] = new CheckingAccount(400, 0.5);

        for (int i = 0; i < 4; i++) {

            double amount;
            string entryString;
            bool check = false;

            Console.WriteLine("Enter an amount to withdraw from Account " + i );
            entryString = Console.ReadLine();
            check = double.TryParse(entryString, out amount);

            while (check == false) {

                Console.WriteLine("Invalid entry. Please specify an amount to withdraw from Account " + i);
                entryString = Console.ReadLine();
                check = double.TryParse(entryString, out amount);

            }

            accounts[i].Debit(amount);

            Console.WriteLine("Enter an amount to deposit into Account " + i);
            entryString = Console.ReadLine();
            check = double.TryParse(entryString, out amount);

            while (check == false) {

                Console.WriteLine("Invalid entry. Please specify an amount to deposit into Account " + i);
                entryString = Console.ReadLine();
                check = double.TryParse(entryString, out amount);

            }

            accounts[i].Credit(amount);

            if (accounts[i] is SavingsAccount) {

                Console.WriteLine("This is a savings account.");

                double interestEarned = accounts[i].CalculateInterest();

                Console.WriteLine("Adding {0} interest to Account {2} (Savings Account)", interestEarned, i);

            }

            else {

                Console.WriteLine("This is a checking account.");

                Console.WriteLine("{0} transaction fee charged.", accounts[i].TransactionFee);

            } 


        }

    }

}

class Account {

    private double balance;

    public double Balance { 

        get {

            return balance;

        }

        set {

            if (value<0) {
                balance = 0;
            }

            else balance = value;

        }

    }

    public Account(double initBalance) {

        Balance = initBalance;

    }

    public void Credit(double amount) {

        Balance = Balance + amount;

    }

    public void Debit(double amount) {

        if (amount>Balance) {

            Console.WriteLine("Amount exceeds account balance.");

        }
        else Balance = Balance - amount;

    }

}

class SavingsAccount : Account {

    private double interestRate;
    private double interestEarned;

    public SavingsAccount(double initBalance, double interest) : base(initBalance) {

        interestRate = interest/100;

    }

    public double CalculateInterest() {

            interestEarned = Balance*interestRate;
            return interestEarned;

    }
}

class CheckingAccount : Account {

    private double transactionFee;

    public double TransactionFee {

        get {
            return transactionFee;
        }

        set {
            transactionFee = value;
        }
    }

    public CheckingAccount(double initBalance, double fee) : base(initBalance) {

        TransactionFee = fee;

    } 

    public new void Credit(double amount) {

        Balance = Balance + amount;
        Balance = Balance - TransactionFee;

    }

    public new void Debit(double amount) {

        bool check;

        if (amount>Balance) {

            Console.WriteLine("Amount exceeds account balance.");
            check = false;

        }
        else {

            Balance = Balance - amount;
            check = true;
        }

        if (check == true) {
            Balance = Balance - TransactionFee;
        }
        else Balance = Balance;
    }

}

1 个答案:

答案 0 :(得分:0)

在使用这些方法之前,首先需要将对象强制转换为更专业的类型:

改变这个:

double interestEarned = accounts[i].CalculateInterest();

对此:

double interestEarned = ((SavingsAccount)accounts[i]).CalculateInterest();

改变这个:

Console.WriteLine("{0} transaction fee charged.", accounts[i].TransactionFee);

对此:

Console.WriteLine("{0} transaction fee charged.", ((CheckingAccount)accounts[i]).TransactionFee);