【问题标题】:Creating a constructor that inherits from a base class and includes an instance variable创建从基类继承并包含实例变量的构造函数
【发布时间】:2020-11-14 16:35:35
【问题描述】:

我正在尝试在派生类中创建一个构造函数,该构造函数从基类获取 initialBalance 和一个实例变量调用 interestRate。我收到错误代码 CS7036,没有给出与“Account.Account(decimal)”所需的形式参数“balance”相对应的参数。

namespace BankApp
{
    class SavingsAccount : Account
    {
        private decimal interestRate;
        private decimal interest;

        public SavingsAccount(decimal percentage, decimal balance) // error is thrown here
        {
            interestRate = percentage;
            IntialBalance = balance;
        }  

        public void CalculateInterest()
        {
           interest = IntialBalance * interestRate;
        }
    }
}
namespace BankApp
{
    class Account
    {
        private decimal accountBalance;

        //constructor 
        public Account(decimal balance)
        {
            IntialBalance = balance; // validate the initial balance in property
        }
        // ensures intial balance is >= 0
        public decimal IntialBalance
        {
            get { return accountBalance; }
            set
            {
                if (value >= 0)
                    accountBalance = value;
                else
                    throw new ArgumentOutOfRangeException("IntialBalance", value, "Intial balance must be >= 0");
            }
        }
        // Returns current balance
        public decimal Balance
        {
            get { return accountBalance; }
        }
        // deposits into account
        public decimal Credit()
        {
            Console.WriteLine("Enter the amount you would like to deposit: ");
            decimal deposit = Convert.ToInt32(Console.ReadLine());

            accountBalance += deposit;
            return accountBalance;
        }
        // withdrawl from account
        public decimal Debit()
        {
            Console.WriteLine("Enter the amount you would like to withdrawl: ");
            decimal withdrawl = Convert.ToInt32(Console.ReadLine());

            if (accountBalance >= withdrawl)
                accountBalance = accountBalance - withdrawl;
            else
                Console.WriteLine("Debit amount exceeded account balance");
            return accountBalance;
        }
    }
}

【问题讨论】:

    标签: c# inheritance constructor derived-class


    【解决方案1】:

    没关系,这就是我想出的。我一直在尝试添加:base(十进制余额),而不仅仅是:base(余额)。如果有更好的方法可以做到这一点,我会很感兴趣。我对编程很陌生。谢谢。

     public SavingsAccount(decimal balance, decimal percentage) : base( balance) 
            {
                interestRate = percentage;
                InitialBalance = balance;
            }
    

    【讨论】:

    • 由于您调用了接收余额的基构造函数,因此您不必自己设置余额,因为这是在基类中为您完成的。
    【解决方案2】:

    出现此错误是因为您的 Account 类没有默认构造函数。当您创建SavingsAccount 的实例时,它首先尝试创建Account 的实例,但不知道为您提供的唯一构造函数的“balance”参数提供什么。

    您可以向Account 类添加一个默认构造函数以消除错误,但您必须手动设置余额。

    最简单的解决方案是调用接收余额的基本构造函数,如下所示:

    public SavingsAccount(decimal percentage, decimal balance) : base(balance)
    {
        interestRate = percentage;            
    }
    

    注意第一行末尾的 : base(balance)!

    另外请注意,我们不再需要这行 IntialBalance = balance;,因为基础构造函数会在调用时分配该值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-24
      • 2015-12-02
      • 2018-06-06
      • 2015-05-06
      • 1970-01-01
      • 2020-12-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多