0

在以下代码中,我不知道使用内部状态标志的目标:(C#)

// internal status flag
    private int status = 0;

    // status constants
    private const int AMOUNT_SET = 1;
    private const int RATE_SET = 2;
    private const int PERIODS_SET = 4;
    private const int ALL_SET = AMOUNT_SET | RATE_SET | PERIODS_SET;
public double LoanAmount
        {
            get
            {
                return this.loanAmount;
            }
            set
            {
                this.loanAmount = Math.Abs(value); //taking the absolute value of the user input to ensure getting  positive values
                this.status |= AMOUNT_SET;
                CalcPayment(); //check if the method can calculate yet 
            }
        }

 public double InterestRate
        {
            get
            {
                return this.interestRate;
            }
            set
            {
                this.interestRate = Math.Abs(value);
                if (this.interestRate >= 1)
                    this.interestRate /= 100;    // if entered as a percent, convert to a decimal
                this.status |= RATE_SET;
                CalcPayment();
            }
        }

        public int Periods
        {
            get
            {
                return this.periods;
            }
            set
            {
                this.periods = Math.Max(Math.Abs(value), 1);
                this.status |= PERIODS_SET;
                CalcPayment();
            }
        }

        public double MonthlyPayment
        {
            get
            {
                return this.monthlyPayment;
            }
        }

        public override string ToString()
        {
            return "\n\tThis is a loan of " + this.loanAmount.ToString("c") + " at " + this.interestRate.ToString("p") +
                    " interest for " + this.periods.ToString() + " months,\n\t\twith a monthly payment of " +
                    this.monthlyPayment.ToString("c") + ".";
        }

        private void CalcPayment()   
        {                              
            if (this.status == ALL_SET)
            {
                double periodicInterestRate = interestRate / 12;
                double compound = Math.Pow((1 + periodicInterestRate), periods);
                this.monthlyPayment = this.loanAmount * periodicInterestRate * compound /                   (compound - 1);
            }
        }

那么为什么它使用状态标志呢?谢谢

4

3 回答 3

2

这只是确保调用了所有必需函数的一种聪明方法。

最初,作为 int 的状态标志在二进制中如下所示:

0000  // truncated for clarity it would really be 32 0's

设置金额后,标志如下所示:

0001 

一旦设置了费率,它看起来像

0011 // because 2 in binary is ...0010, so ORing 0001 and 0010 -> 0011
于 2013-11-06T02:22:20.963 回答
0

我想最初的意图是只有在设置所有字段时才进行计算。该标志很重要,因为 double 和 int 都是默认值分别为 0.0d 和 0 的值类型。

我可以看到这是在 C# 具有 Nullable 结构或捷径 T 之前完成的?包含在语言中的位置。

于 2013-11-06T02:23:20.587 回答
0

我会说这是easier使用

 if (this.status == ALL_SET)

然后有 3 个可为空的并在做(伪代码)

 if (this.LoanAmount.HasValue && this.InterestRate.HasValue && this.Periods.HasValue)

我认为代码偏好

于 2013-11-06T02:24:05.513 回答