构造循环创建对象

本文关键字:创建对象 循环 | 更新日期: 2023-09-27 18:27:42

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CodyGarrettEX3
{
    public class Account
    {
        private long acctNumber;
        protected double balance;
        public Savings SavingsAccount;
        public Checking CheckingAccount;
        public Account()
        {
            this.acctNumber = 1234;
            Savings SavingsAccount = new Savings(acctNumber);
            Checking CheckingAccount = new Checking(acctNumber);
        }
        public Account(long newAcctNo)
        {
            this.acctNumber = newAcctNo;
            Savings SavingsAccount = new Savings(newAcctNo);
            Checking CheckingAccount = new Checking(newAcctNo);
        }
        //Members
        public long AcctNo
        {
            get { return AcctNo; }
            set { AcctNo = value; }
        }
        public double Balance
        {
            get { return balance; }
            set { balance = value; }
        }
        public virtual double Withdrawal(double amount)
        {
            if ((balance - amount) < 0)
            {
                PolicyException insufficientFundsException = new PolicyException("Insufficient Funds To Complete This Transaction");
                throw insufficientFundsException;
            }
            return balance;
        }
        public double Deposit(double amount)
        {
            balance = balance + amount;
            return balance;
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CodyGarrettEX3
{
    public class Savings: Account
    {
        private double interestRate;
        private double minBalance;
        public Savings(long newAcctNumber):base(newAcctNumber)
        {
            balance = 0;
            interestRate = 0;
            minBalance = 0;
        }
        //Properties
        public double InterestRate //This is a correctional to ensure that the interest rate is always stored as a decimal for math calculations
        {
            get { return interestRate; }
            set 
            { 
                if (value > 0)
                {
                    value = value / 100;
                }
                interestRate = value;
            }
        }
        public double MinBalance
        {
            get { return minBalance; }
            set { minBalance = value; }
        }
        public override double Withdrawal(double amount)
        {
            if (minBalance > 0)
            {
                if (amount > (balance - minBalance))
                {
                    PolicyException minBalanceException = new PolicyException("Insufficient Funds to Complete This Transaction, Exceeds Account Balance and Minimum Balance of Account");
                    throw minBalanceException;
                }
            }
            else
            {
                if(amount > (balance - amount))
                {
                    PolicyException insufficientFundsException = new PolicyException("Insufficient Funds to Complete This Transaction, Exceeds Account Balance");
                    throw insufficientFundsException;
                }
            }
            return balance;
        }
    }
}

看起来的主要问题是,在编译操作我的父级(即帐户类(的类时,我得到了一个非常奇怪的循环,它只是从帐户构造函数快速转到储蓄构造函数,然后导致堆栈溢出。

这发生在我的程序测试类中执行"帐户银行帐户 = 新帐户(9999(;"之后。

非常感谢大家的帮助!我真的很难弄清楚这里的原因是什么。

另外,旁注我以这种方式构建构造函数,因为赋值中要求我们必须让 acctNumber 变量传递到对象中,然后才能创建它。即使对象不使用该值。但是,如果这是不可能的,我真的对任何事情都持开放态度。

构造循环创建对象

循环并不那么奇怪。在储蓄中,您从帐户继承。储蓄的构造函数也调用帐户的构造函数。

帐户的构造函数创建一个新的"储蓄"对象。此新储蓄对象的构造函数希望将数据传递给 Accounts 的构造函数,后者想要创建新的储蓄对象。啪��这种情况会一直持续下去。

它看起来像基类(帐户(创建(并知道(继承类(如储蓄(的代码气味。

编辑:我不知道您的要求的细节,但是当我看到这个时,设计不适合您的问题。

您有银行账户,此银行账户可以是储蓄型账户。

class Account
{
   protected int _accNr;
   public Account()
   {
      _accnr = 1234;
   }       
   public Account(int accNr)
   {
      _accNr = accNr;
      // only do basic 'account' stuff
   }
}
class Savings : Account
{
   public Savings() : base()
   {
      // do other stuff
   }
   public Savings(int accNr) : base(accNr)
   {
      // do other stuff
   }
}

现在,我只设置了构造函数,没有其他任何设置。关键是,现在您可以通过调用以下命令创建具有或没有帐户nr的储蓄类:

Account a = new Savings();

Account b = new Savings(9999);

现在,责任分为账户类和储蓄类。帐户类不想了解继承类及其职责。通过多态性,我们可以创建"储蓄"类型的帐户,但您也可以忽略它并执行Savings a = new Savings(9999)