在数组而不是数组类型中调用对象类型的方法

时间:2019-02-19 20:34:05

标签: c# arrays object derived-class

我有一个父类account,有两个派生的SavingsAccountCheckingsAccount。这些子类具有各自的方法变体。我想调用存储在数组中的对象的方法。因此,类型为Account的数组包含类型为SavingsAccountCheckingsAccount的对象。

我认为我可以通过进行Account[0].CalculateInterest

来称呼它

CalculateInterestSavingsAccount内部的方法中。

This error is thrown

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CustomersPartTwo
{
    class Program
    {
        static void Main(string[] args)
        {
            Account[] customerAccounts = new Account[4];
            decimal temp = 0;

            customerAccounts[0] = new SavingsAccount(25, 0.03m);
            customerAccounts[1] = new SavingsAccount(200, 0.015m);
            customerAccounts[2] = new CheckingAccount(80, 1);
            customerAccounts[3] = new CheckingAccount(400, 0.5m);


            Console.WriteLine("Account 1 balance: " + customerAccounts[0].Balance.ToString("C"));

            Console.WriteLine("Enter an amount to withdraw: ");
            temp = Convert.ToDecimal(Console.ReadLine());
            customerAccounts[0].Debit(temp);

            Console.WriteLine("Enter an amount to deposit: ");
            temp = Convert.ToDecimal(Console.ReadLine());
            customerAccounts[0].Credit(temp);

            if (customerAccounts[0].GetType() == typeof(SavingsAccount))
            {
                customerAccounts[0].Credit(SavingsAccount.CalculateInterest());
            }

        }


        /* Base Class ------------------------------------*/
        class Account
        {
            private decimal balance;

            /* Property */
            public decimal Balance
            {
                get { return balance; }
                set
                {
                    if (value < 0)
                        balance = 0;
                    else
                        balance = value;
                }
            }

            /* Constructor */
            public Account(decimal initialBalance)
            {
                Balance = initialBalance;
            }

            /* Method to add to balance */
            public void Credit(decimal addedFunds)
            {
                Balance += addedFunds;
            }

            /* Method to subtract from balance */
            public void Debit(decimal subtractedFunds)
            {
                if (subtractedFunds > Balance)
                    Console.WriteLine("Debit amount exceeds balance amount");
                else
                    Balance -= subtractedFunds;
            }
        }
        /* Base Class ------------------------------------*/


        class SavingsAccount : Account
        {
            private decimal interestRate;

            /* Constructor */
            public SavingsAccount(decimal initialBalance, decimal rateOfInterest) : base(initialBalance)
            {
                interestRate = rateOfInterest;
            }

            /* Method to calculate interest */
            public decimal CalculateInterest()
            {
                decimal earnedInterest = 0;

                earnedInterest = Balance * interestRate;

                return earnedInterest;
            }
        }


        class CheckingAccount : Account
        {
            private decimal transactionFee;

            /* Constructor */
            public CheckingAccount(decimal initialBalance, decimal usageFee) : base(initialBalance)
            {
                transactionFee = usageFee;
            }

            /* Method to add to balance */
            public new void Credit(decimal addedFunds)
            {
                Balance += addedFunds;
                Balance -= transactionFee;
            }

            /* Method to subtract from balance */
            public new void Debit(decimal subtractedFunds)
            {
                if (subtractedFunds > Balance)
                    Console.WriteLine("Debit amount exceeds balance amount");
                else
                    Balance -= (subtractedFunds + transactionFee);              
            }

        }
    }
}

1 个答案:

答案 0 :(得分:2)

您可以这样尝试吗?

if (customerAccounts[0] is SavingAccount sa)
{
    customerAccounts[0].Credit(sa.CalculateInterest());
}