为什么使用内部状态标志? C#

时间:2013-11-06 02:13:41

标签: c# bit-manipulation flags

在下面的代码中,我不知道使用内部状态标志的目标:(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);
            }
        }

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

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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

我可以看到在C#有Nullable结构或捷径T之前完成了这个吗?包含在语言中。

答案 2 :(得分:0)

我会说easier使用

 if (this.status == ALL_SET)

然后有3个可空和做(伪类代码)

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

代码偏好我认为