将字符串转换为Enum类 [英] Converting a string to the Enum class

查看:126
本文介绍了将字符串转换为Enum类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

免责声明:我不会说我是最有经验的Java人。在我的例子中可以有更简单的方法来做我自己。但这是一个关于将字符串条目转换为Enum类而不是枚举变量的问题。我试图尽可能地解释。

Disclaimer: I am not going to say that I am the most experienced Java person. There could be easier ways to do what I have in my examples. But this is a question about turning a string entry into an Enum class, not an enum variable. I tried to explain as best as I could.

这是一个我想做的简单版本。我将有两个不同的枚举,我想根据用户想要的选择一个或另一个。例如,在代码中,将是

This is a simple version of what I want to do. I will have two different enums and I want to choose one or the other based off what the user wants. And example of this, in code, would be

public enum Letters {
    A, B, C, D, E, ... , X, Y, Z
}

public enum Numbers {
    ONE, TWO, THREE, ..., EIGHT, NINE, TEN
}

public static void main(String [] args) {

    System.out.println("Enter in you choice, letters (Letters) or numbers(Numbers)");
    String entry = "";
    InputStreamReader input = new InputStreamReader(System.in);
    BufferedReader reader = new BufferedReader(input);
    try {
        entry = reader.readLine();
    }
    catch(Exception e){};
    ...
}

我想让它成为用户,如果他输入信件,它将能够获取字符串条目,并将其转换为字母枚举的变量引用。椭圆是代码的占位符。我想最终使用这个,所以我可以使用

I want to make it so the user, if he enters in "Letters" it will be able to take the string entry and turn it into a variable reference to the Letters Enum. The ellipses are place holders for code. I want to eventually use this so I can use

for (answer n : variable.values()) {
    System.out.println(n);
}

而不是

if (answer.equals("Numbers")) {
    Numbers n;
    for (n : Numbers.values()) {
        System.out.println(n);
    }
} else {
    Letters l;
    for (l : Letters.values()) {
        System.out.println(l);
    }
}

我尝试过

Class<?> c = Class.forName("Letters");

但不行。

在我现实世界的例子中,我有3个.java文件,其中包含单独的类和每个类的枚举。有三明治,啤酒和筹码类,他们的枚举是三明治,啤酒,薯条。这些类实现了VendingMachineItem接口。最后,该接口是一个vendingMachine包的一部分。我不知道这些是否重要,但这是我可以提供的所有信息。

In my real world example, I have 3 .java files which contains separate classes, and an enum in each of them. There is Sandwich, Beer, and Chip class and their enums are Sandwiches, Beers, Chips. Those classes implement an interface VendingMachineItem. And finally, that interface is part of a vendingMachine package. I don't know if those matter, but that's all the info that I can give.

推荐答案

以下作品Java6在Eclipse下):

The following works (tested with Java6 under Eclipse):

package com.bob;
public class Test
{
    public enum Letters {
        A, B, C, D, E
    }

    public enum Numbers {
        ONE, TWO, THREE, FOUR, FIVE
    }

    public static void main(String [] args) throws Exception 
    {

        System.out.println("Enter in you choice, letters (Letters) or numbers(Numbers)");
        String entry = "";
        InputStreamReader input = new InputStreamReader(System.in);
        BufferedReader reader = new BufferedReader(input);
        entry = reader.readLine();

        Class<Enum<?>> clazz = (Class<Enum<?>>)Class.forName("com.bob.Test$" + entry);

        for (Enum<?> x : clazz.getEnumConstants())
        {
            System.out.println(x);
        }

    }
}

请注意枚举的完全限定类名为 com.bob.Test $ Letters com.bob.Test $ Numbers due成为内部班。错误处理作为一个练习。

Notice that the fully qualified classname of the enums are com.bob.Test$Letters and com.bob.Test$Numbers due to being inner classes. Error handling is left as an exercise.

这篇关于将字符串转换为Enum类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆