如何遍历显示一个枚举的内容

此博客展示了C#中枚举类型的使用。定义了表示星期、沸点和颜色的枚举,通过循环输出枚举项及其对应值,还演示了使用FlagsAttribute组合枚举值。体现了枚举在C#编程中表示一组常量的应用。

using System;

public class EnumTest {
    enum Days { Saturday, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday };
    enum BoilingPoints { Celcius = 100, Fahrenheit = 212 };
    [FlagsAttribute]
    enum Colors { Red = 1, Green = 2, Blue = 4, Yellow = 8 };

    public static void Main() {

        Type weekdays = typeof(Days);
        Type boiling = typeof(BoilingPoints);

        Console.WriteLine("The days of the week, and their corresponding values in the Days Enum are:");

        foreach ( string s in Enum.GetNames(weekdays) )
            Console.WriteLine( "{0,-11}= {1}", s, Enum.Format( weekdays, Enum.Parse(weekdays, s), "d"));

        Console.WriteLine();
        Console.WriteLine("Enums can also be created which have values that represent some meaningful amount.");
        Console.WriteLine("The BoilingPoints Enum defines the following items, and corresponding values:");

        foreach ( string s in Enum.GetNames(boiling) )
            Console.WriteLine( "{0,-11}= {1}", s, Enum.Format(boiling, Enum.Parse(boiling, s), "d"));

        Colors myColors = Colors.Red | Colors.Blue | Colors.Yellow;
        Console.WriteLine();
        Console.WriteLine("myColors holds a combination of colors. Namely: {0}", myColors);
    }
}

在不同的编程语言中,遍历枚举类的方法有所不同,下面以 C# 和 Java 为例进行说明。 ### C# 中遍历枚举类 在 C# 中,可以定义一个泛型方法来遍历枚举类型。以下是示例代码: ```csharp using System; class Program { // 定义一个泛型方法来打印枚举的值 public static void PrintEnumValues<TEnum>() where TEnum : Enum { // 获取枚举的所有值 Array enumValues = Enum.GetValues(typeof(TEnum)); // 遍历枚举值 foreach (TEnum value in enumValues) { // 输出枚举成员的名称和对应的整数值 Console.WriteLine($"Name: {value.ToString()}, Value: {(int)(object)value}"); } } // 定义一个示例枚举 enum ExampleEnum { Value1, Value2, Value3 } static void Main() { // 调用泛型方法打印枚举值 PrintEnumValues<ExampleEnum>(); } } ``` 在上述代码中,定义了一个泛型方法 `PrintEnumValues<TEnum>`,其中 `TEnum` 必须是一个枚举类型。使用 `Enum.GetValues` 获取所有枚举值的数组,然后通过 `foreach` 循环遍历这些值,并通过 `ToString()` 方法获取枚举成员的名称,将其转换为 `(int)value` 来显示实际的整数值[^1]。 ### Java 中遍历枚举类 在 Java 中,可以直接使用 `values()` 方法来遍历枚举类。以下是示例代码: ```java public class EnumTraversal { // 定义一个枚举类 public enum StatusType { UNKNOWN(0, "未知"), NORMAL(1, "正常"), OFFLINE(2, "离线"), POWER_FAILURE(38, "电源断电"); // 定义属性 private int code; private String name; // 构造方法 private StatusType(int code, String name) { this.code = code; this.name = name; } // get 和 set 方法 public int getCode() { return code; } public String getName() { return name; } // 根据 code 获取对应的 name public static String getValueBykey(int code) { try { for (StatusType type : StatusType.values()) { if (type.getCode() == code) { return type.getName(); } } } catch (Exception e) { return "未知"; } return "未知"; } } public static void main(String[] args) { // 遍历枚举类 for (StatusType type : StatusType.values()) { System.out.println("Code: " + type.getCode() + ", Name: " + type.getName()); } } } ``` 在上述 Java 代码中,定义了一个枚举类 `StatusType`,包含 `code` 和 `name` 两个属性。通过 `StatusType.values()` 方法获取枚举类的所有实例,然后使用 `for-each` 循环遍历这些实例,输出每个实例的 `code` 和 `name` [^2]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值