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

    }
}

The main issue as it would seem is that upon compilation of the class that manipulates my parent Which is the Account Class is that I get a really odd loop that just goes from the Account Constructor to the Savings Constructor rapidly and then causes a stack overflow.

This happens after the execution of "Account BankAccount = new Account(9999);" in my program testing class.

Thank you so much for your help everyone! I'm really struggling to figure out what is the cause here.

Also, side note I have the constructor built that way due to a requirement in an assignment that mandates we have to have the acctNumber variable pass into the object before it can be created. Even if the object doesn't use the value. However, if this is not possible I am open to anything really.

4

1 回答 1

1

循环不是那么奇怪。在 Savings 中,您从 Account 继承。Savings 的构造函数也调用 Account 的构造函数。

Account 的构造函数创建一个新的“Savings”对象。这个新的 Savings 对象的构造函数想要将数据传递给 Accounts 的构造函数,后者想要创建一个新的 Savings 对象。冲洗并重复。这将继续下去。

它看起来像是基类(Account)创建(并知道)继承类(如 Savings)的代码异味。

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

你有你的银行账户,这个银行账户可以是储蓄型账户。

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 的 Savings 类:

Account a = new Savings();

或者

Account b = new Savings(9999);

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

于 2014-10-23T08:44:30.827 回答