简体   繁体   English

将对象转换为枚举JAVA

[英]cast object to enum JAVA

I have to iterate over myEnum.values() with only knowing that myEnum implement specific interface lets assume that I have interface statsInterface I have few enums that implement this interface 我必须遍历myEnum.values() ,只知道myEnum实现特定的interface让我们假设我有接口statsInterface我只有很少的枚举来实现此接口

enum statsfromMachine1
enum statsfromMachine2
..

later I want to iterate over . 后来我想遍历。 what am I thinking of is somthing like 我在想什么

public void interfaceEnumImplParser (statsInterface _interface ,object _myinterfaceImplEnum, string Input) {
    for ((_interface)_myinterfaceImplEnum iterable_element : _myinterfaceImplEnum.values()) {
        //do stuff here
    }
}

But I'm missing something here and it won't even compile Is it possible to do? 但是我在这里丢失了一些东西,甚至无法编译。可以吗?

Looks like you want to cast the enum into the interface type directly in iteration. 看起来您想直接在迭代中将enum转换为接口类型。 If that is true then you should modify your code into something like this. 如果是这样,则应将代码修改为如下所示。

public void interfaceEnumImplParser (statsInterface _interface ,object _myinterfaceImplEnum, string Input) {
    for (_interface iterable_element : _myinterfaceImplEnum.values()) {
        //do stuff here
    }
}

hope that helps. 希望能有所帮助。

I wrote a small sample that could help you find a common implementation logic for multiple enums. 我写了一个小样本,可以帮助您为多个枚举找到通用的实现逻辑。

interface Doable<B>{
    B doStuff(Number a);

    String getCategory()
}


enum StatsFromMachine1 implements Doable<String> {
    ABC{

        @Override
        public String doStuff(Number a) {               
            return Integer.toString(a.intValue() + 3);
        }

    };

    @Override
    public String getCategory() {
        return "S1";
    }
}

enum StatsFromMachine2 implements Doable<String>{
    X(1.4), Y(2.86), Z(0.1);

    private Double d;

    @Override
    public String doStuff(Number x) {
        return Double.toString(x.doubleValue() * this.d);
    }

    private StatsFromMachine2(Double d) {
        this.d = d;
    }

    @Override
    public String getCategory() {
        return "S2";
    }
}

When the enums implement the same interface, then it's easy to write code that is applicable to all of them. 当枚举实现相同的接口时,很容易编写适用于所有枚举的代码。 Actually, this kind of abstraction doesn't care, whether the implementation is given by an enum or something else. 实际上,无论实现是由枚举还是其他方式提供,这种抽象都无关紧要。

public static String doStuffWithEnums(){
    Collection<Doable<String>> collection = Arrays.asList(StatsFromMachine1.values());
    collection.addAll(Arrays.asList(StatsFromMachine2.values()));

    StringBuilder sb = new StringBuilder();

    for(Doable<String> d : collection){
        sb.append(d.doStuff(5.0));

        if(d.getCategory().equals("S1")){ // alternatively use an instanceOf check
            sb.append(((StatsFromMachine1)d).name());
        }
    }
    return sb.toString();
}

OK I've figured it out Thank you for the help and you will get +1 after I review your content :) so basically I have enum E1 that implement interface I1 To iterate over the enum members "generically" what I did is : 好的,我已经弄清楚了,谢谢您的帮助,在查看您的内容后,您将获得+1 :)因此,基本上,我有实现interface I1 enum E1以“一般”方式遍历enum成员:

public static void parse_log_with_enum(I1 ... Is)
{
    for (I1 i1 : Is) {
        // do something with i1
    }   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM