【问题标题】:Call a method of Object type in array, not array type在数组中调用 Object 类型的方法,而不是数组类型
【发布时间】:2019-07-13 10:39:03
【问题描述】:

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

我想我可以通过 Account[0].CalculateInterest 调用它

CalculateInterestSavingsAccount 内部的方法中。

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);              
            }

        }
    }
}

【问题讨论】:

  • 您需要在Account 的实例上调用CalculateInterest,例如customerAccounts[0].Credit(customerAccounts[0].CalculateInterest())。你能告诉我们你的Account课程吗? CalculateInterest 是否也存在于此类中?如果没有,您可能需要将您的实例转换为SavingsAccount
  • 您还可以考虑支票账户和储蓄账户之间是否真的存在差异,除了基于规则(基于财产)的差异(如最低余额、最高取款、维护费用等) .),可以将其指定为实例的属性。如果是这种情况,那么只需要一个BankAccount 类,然后创建它的实例,比如BankAccount checkingAccount = new BankAccount();,这样你的方法只需要担心一个类型。
  • @Jeppe 我试过这个: if (customerAccounts[x].GetType() == typeof(SavingsAccount)) { temp = customerAccounts[x].CalculateInterest(); customerAccounts[x].Credit(temp); } 还是行不通。似乎它没有确认数组内对象的数据类型,或者其他什么。收到“不包含定义”错误。

标签: c# arrays object derived-class


【解决方案1】:

你可以这样试试吗?

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

【讨论】:

    猜你喜欢
    • 2020-02-26
    • 1970-01-01
    • 2011-04-27
    • 2020-01-31
    • 1970-01-01
    • 1970-01-01
    • 2016-10-11
    • 2022-01-02
    • 2021-06-28
    相关资源
    最近更新 更多