How to cast a value from one enum to another in Java?
问题 How can I cast a value from Enum1 to Enum 2 in Java? Here is an example of what I'm trying to do : public enum Enum1 { ONE, TWO, THREE; } public enum Enum2 { FOUR, FIVE, SIX; } So I want to do something like this: Enum2 en2 = (Enum2)ONE; Is it possible and how can I do that? Thanks in advance! 回答1: You cannot cast from one enum to another, however each enum has guaranteed order, and you can easily translate one enum to another (preserving order). For example: enum E1 { ONE, TWO, THREE, } enum