Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > C/C++ > 通用类型系统中的枚举(enum<位域>)
【标  题】:通用类型系统中的枚举(enum<位域>)
【关键字】:enum
【来  源】:http://www.cublog.cn/u/884/showart.php?id=98363

通用类型系统中的枚举(enum<位域>)

Your Ad Here

枚举 (enum) 是值类型的一种特殊形式,它从 System.Enum 继承而来,并为基础基元类型的值提供替代名称。枚举类型有名称、基础类型和一组字段。基础类型必须是一个内置的有符号(或无符号)整数类型(如 ByteInt32UInt64)。字段是静态文本字段,其中的每一个字段都表示常数。同一个值可以分配给多个字段。出现这种情况时,必须将其中某个值标记为主要枚举值,以便进行反射和字符串转换。

可以将基础类型的值分配给枚举,反之亦然(运行库不要求强制转换)。可创建枚举的实例,并调用 System.Enum 的方法以及对枚举的基础类型定义的任何方法。但是,某些语言可能不允许在要求基础类型的实例时作为参数传递枚举(反之亦然)。

对于枚举还有以下附加限制:

  • 它们不能定义自己的方法。

  • 它们不能实现接口。

  • 它们不能定义属性或事件。

  • 除非枚举只是因为嵌套在泛型类型中因而成为泛型,否则枚举不能为泛型。也就是说,枚举不能有自己的类型参数。

    Note注意

    用 Visual Basic、C# 和 C++ 创建的嵌套类型(包括枚举)包含所有封闭泛型类型的类型参数,因此即使这些嵌套类型没有自己的类型参数,它们也是泛型的。有关更多信息,请参见 MakeGenericType 中的“嵌套类型”。

    可以在 Microsoft 中间语言 (MSIL) 程序集语言中声明泛型枚举,但如果尝试使用该枚举,则会引发 TypeLoadException

Flags 属性表示一种特殊的枚举,称为位域。运行库本身不区分传统枚举与位域,但您的语言可能会区分二者。当区分二者的时候,可以对位域(而不是枚举)使用位操作符以产生未命名的值。枚举一般用于列出唯一的元素,如一周的各天、国家/地区名称,等等。位域一般用于列出可能联合发生的质量或数量,比如 Red And Big And Fast

下面的示例说明如何使用位域和传统枚举。

Imports System
Imports System.Collections

' A traditional enumeration of some root vegetables.
Public Enum SomeRootVegetables
    HorseRadish
    Radish
    Turnip
End Enum 'SomeRootVegetables


' A bit field or flag enumeration of harvesting seasons.
<Flags()> Public Enum Seasons
    None = 0
    Summer = 1
    Autumn = 2
    Winter = 4
    Spring = 8
   All = Summer Or Autumn Or Winter Or Spring
End Enum 'Seasons


' Entry point.
Public Class EnumerationSample
    
    Public Shared Sub Main()
        ' Hash table of when vegetables are available.
        Dim AvailableIn As New Hashtable()
        
        AvailableIn(SomeRootVegetables.HorseRadish) = Seasons.All
        AvailableIn(SomeRootVegetables.Radish) = Seasons.Spring
        AvailableIn(SomeRootVegetables.Turnip) = Seasons.Spring Or 
            Seasons.Autumn
        
        ' Array of the seasons, using the enumeration.
        Dim MySeasons() As Seasons = {Seasons.Summer, Seasons.Autumn, 
            Seasons.Winter, Seasons.Spring}
        
        ' Print information of what vegetables are available each season.
        Dim i As Integer
        For i = 0 To MySeasons.Length - 1
            Console.WriteLine("The following root vegetables are harvested in 
                " + MySeasons(i).ToString("G") + ":")
            Dim e As DictionaryEntry
            For Each e In  AvailableIn
                ' A bitwise comparison.
                If(CType(e.Value, Seasons) And MySeasons(i)) > 0 Then
                    Console.WriteLine(CType(e.Key, 
                        SomeRootVegetables).ToString("G"))
                End If
            Next e
        Next i
    End Sub 'Main
End Class 'EnumerationSample
using System;
using System.Collections;

// A traditional enumeration of some root vegetables.
public enum SomeRootVegetables
{
    HorseRadish,
    Radish,
    Turnip
}

// A bit field or flag enumeration of harvesting seasons.
[Flags]
public enum Seasons
{
    None = 0,
    Summer = 1,
    Autumn = 2,
    Winter = 4,
    Spring = 8
    All = Summer | Autumn | Winter | Spring,
}

// Entry point.
public class EnumerationSample
{
    public static void Main()
    {
        // Hash table of when vegetables are available.
        Hashtable AvailableIn = new Hashtable();

        AvailableIn[SomeRootVegetables.HorseRadish] = Seasons.All;
        AvailableIn[SomeRootVegetables.Radish] = Seasons.Spring;
        AvailableIn[SomeRootVegetables.Turnip] = Seasons.Spring | 
            Seasons.Autumn;

        // Array of the seasons, using the enumeration.
        Seasons[] seasons = new Seasons[] { Seasons.Winter, Seasons.Spring, 
            Seasons.Summer, Seasons.Autumn };

        // Print information of what vegetables are available each season.
        for (int i = 0; i < seasons.Length; i++)
        {
            Console.WriteLine("\r\nThe following root vegetables are 
                harvested in " + seasons[i].ToString("G") + ":");
            foreach (DictionaryEntry e in AvailableIn)
            {
                // A bitwise comparison.
                if (((Seasons)e.Value & seasons[i]) > 0)
                    Console.WriteLine("\t" + 
                        ((SomeRootVegetables)e.Key).ToString("G"));
            }
        }
    }
}

此程序的输出如下所示。

The following root vegetables are harvested in Summer:
HorseRadish
The following root vegetables are harvested in Autumn:
Turnip
HorseRadish
The following root vegetables are harvested in Winter:
HorseRadish
The following root vegetables are harvested in Spring:
Turnip
Radish
HorseRadish

请参见

一个实现字符串中单词顺序反转的小例子。。。:【上一篇】
c调试和优化:【下一篇】
【相关文章】
  • 14.9.5 Enumeration comparison operators
  • Enums and Structs in C#
  • WEB Service 中使用 Enum 类型。
  • 11.1.8 Enumeration types
  • .NET Quick Tip: Runtime Type Conversion for Enumeration
  • 8.11 Enums
  • 在C#中调用Win32函数EnumWindows枚举所有窗口
  • 取得MYSQL中ENUM(枚举)列的全部可能值
  • 初探c#(十一)枚举(Enums)
  • enum类型的重载操作
  • 【随机文章】
  • windows xp + XManager2 + Red Hat Linux9
  • 一个odbc连mssql分页的类
  • 华为26新命令行 最简单配置
  • 河北黑客操控6万台电脑制造网络“僵尸”
  • proc文件系统
  • Rainbow分页解决方案
  • SOA在一个典型企业应用案例中的应用构想
  • php&java(二)
  • 2004免费网络相册年终大盘点
  • C Review1
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.