zoukankan      html  css  js  c++  java
  • 标记枚举(flags)的使用

        标记枚举是为标记相互包含而使用的,每个成员被赋给一个唯一的按位值。
        用按位或运算符(|)来组合按位标记。用按位和运算符(&)来确定一个标记的存在。
        从组合状态中去掉一个元素用& (~)。
    示例代码:

     1using System;
     2using System.Collections.Generic;
     3using System.Text;
     4
     5namespace ConsoleApplication1
     6{
     7    class Program
     8    {
     9        [Flags]
    10        public enum Contribution
    11        {
    12            Pension=0x01,
    13            ProfitSharing=0x02,
    14            CreditBureau=0x04,
    15            SavingsPlan=0x08,
    16            All = Pension | ProfitSharing | CreditBureau | SavingsPlan
    17        }

    18        public class Employee
    19        {
    20            private Contribution prop_contributions;
    21            public Contribution contributions
    22            {
    23                get return prop_contributions; }
    24                set { prop_contributions = value; }
    25            }

    26        }

    27
    28        public class Starter
    29        {
    30            public static void Main()
    31            {
    32                Employee bob = new Employee();
    33                bob.contributions = Contribution.ProfitSharing | Contribution.CreditBureau;
    34                if ((bob.contributions & Contribution.ProfitSharing) == Contribution.ProfitSharing)
    35                {
    36                    Console.WriteLine("Bob enrolled in profit sharing");
    37                    Console.WriteLine(bob.contributions);
    38                    bob.contributions = bob.contributions & (~Contribution.ProfitSharing);
    39                    Console.WriteLine(bob.contributions);
    40                }

    41            }

    42        }

    43    }

    44}
  • 相关阅读:
    那些年,加班搞过的需求...
    在基于nuxt的移动端页面中引用mint UI的popup组件之父子组件传值
    nuxt下运行项目时内存溢出(out of memory)的一种情况
    转载的
    VUE旅程(2)
    用windbg+sos找出程序中谁占用内存过高,谁占用CPU过高(转载)
    分块算法总结(持续更新ing)
    【MATLAB建模学习实录【三】】插值与拟合、数据处理
    【MATLAB建模学习实录【二】】非线性规划函数模型板子
    【MATLAB建模学习实录【一】】MATLAB求解线性规划模板
  • 原文地址:https://www.cnblogs.com/fang8206/p/1830133.html
Copyright © 2011-2022 走看看