Tag: 枚举

Java Enum作为Enum中的generics类型

我正在尝试在抽象类中创建一个抽象方法,该方法将我自己的Enum作为参数。 但我也希望Enum是通用的。 所以我宣布它是这样的: public abstract <T extends Enum> void test(Enum command); 在实现中,我已经知道了那个: public enum PerspectiveCommands { PERSPECTIVE } 并且方法声明变为: @Override public <PerspectiveCommands extends Enum> void test(Enum command) { } 但如果我这样做: @Override public <PerspectiveCommands extends Enum> void test(Enum command) { if(command == PerspectiveCommands.PERSPECTIVE){ //do something } } 我没有访问带有错误的PerspectiveCommands.PERSPECTIVE : cannot find symbol symbol: variable PERSPECTIVE location: […]

当枚举类型是一个类时如何使用Javareflection?

我正在使用枚举,其中常量是一个类。 我需要在常量上调用一个方法但不能引入编译时依赖项,并且枚举在运行时并不总是可用(可选安装的一部分)。 因此,我想用reflection。 这很简单,但我之前没有使用枚举reflection。 枚举看起来像这样: public enum PropertyEnum { SYSTEM_PROPERTY_ONE(“property.one.name”, “property.one.value”), SYSTEM_PROPERTY_TWO(“property.two.name”, “property.two.value”); private String name; private String defaultValue; PropertyEnum(String name) { this.name = name; } PropertyEnum(String name, String value) { this.name = name; this.defaultValue = value; } public String getName() { return name; } public String getValue() { return System.getProperty(name); } public String getDefaultValue() […]

Java:枚举常量中方法和变量的定义

我正在做一些实验并且意外地编写了一个代码,这非常奇怪而且我不能完全理解。 我甚至感到惊讶,我可以编译它。 它看起来像这样: enum Foo { VALUE_1 { public int myVariable = 1; }, VALUE_2 { public void myMethod() { // } }, VALUE_3; } 正如预期的那样,不可能通过以下方式访问这样的元素: Foo.VALUE_2.myMethod(); 原因是,编译器将在枚举本身内查找该方法。 我假设不可能从枚举之外访问这些方法和变量。 出于这个原因,我尝试创建一个参数化构造函数并使用一些内部变量调用它: enum Foo { VALUE(internalVariable) { int internalVariable = 1; }; private Foo(int param) { // } } 编译这样的结构是不可能的。 现在我在想如果没有办法访问它,那么在常量中定义一些内容是什么意思。 我试图在常量中创建同名的方法以及枚举本身,以检查它是否以某种方式发生碰撞。 它没有! enum Foo { VALUE_1 […]

将String与枚举值进行比较的正确方法是什么?

家庭作业:Rock Paper Scissors游戏。 我创建了一个枚举: enum Gesture{ROCK,PAPER,SCISSORS}; 从中我想比较价值来决定谁赢 – 计算机或人。 设置值很好,比较工作正常(纸张覆盖岩石,岩石压碎剪刀,剪刀剪纸)。 但是,我不能让我的领带工作。 只要有平局,用户就会被宣布为获胜者。 啊……废话……这将澄清: userPick是一个带有价值rock , paper或scissors的String 。 我无法使用==来比较userPick和computerPick ,正如你在下面看到的那样,它是从我的enum为Gesture类型。 if(computer == 1) computerPick = Gesture.ROCK; else if(computer == 2) computerPick = Gesture.PAPER; else computerPick = Gesture.SCISSORS; if(userPick.equals(computerPick)) { msg = “tie”; ++tieGames; } etc…. 我猜测rock不等于ROCK ,或者String userPick无法匹配Gesture computerPick因为后者不是String 。 但是,我无法在我的教科书或Oracle的Java教程中找到类似情况的例子,所以我不确定如何纠正这个问题…… 任何提示?

在java中,这样的枚举类型编译成什么?

下面是定义枚举类型的代码。 enum Company{ EBAY(30), PAYPAL(10), GOOGLE(15), YAHOO(20), ATT(25); private int value; private Company(int value){ super(this.name()); this.value = value; } public int getValue(){ return value; } } 内部编译到, final class Company extends Enum{ public final static Company EBAY = new Company(30); public final static Company PAYPAL = new Company(10); public final static Company GOOGLE = new […]

通过JAXB为枚举提供自定义值序列化

对于我正在进行的项目,我们使用了很多枚举。 模型对象本身由许多小类组成; 然后,我们通过JAXB将此模型作为XML序列化到我们的DB。 现在,我们希望能够使用枚举中特定方法的返回来序列化我们的枚举值; 给出的: public enum Qualifier { FOO(“1E”, “Foo type document”), BAR(“2”, “Bar object”); private String code, description; public Qualifier(String code, String description) { this.code = code; this.description = description; } public String getCode() { return this.code; } public String getDescription() { return this.description; } } 目前,当序列化为XML时,我们得到类似的东西: FOO 这是JAXB处理它的方式。 但是,我们需要将值作为getCode()的返回值,并且我们的许多枚举都遵循该约定(使用相应的静态方法通过代码进行查找),因此上面的XML片段如下所示: 1E 代替。 我们可以使用@XmlEnum和@XmlEnumValue来注释它,但这太繁琐了 […]

Java:方法中的枚举参数

我有一个方法可以说: private static String drawCellValue( int maxCellLength, String cellValue, String align) { } 你可以注意到,我有一个名为align的参数。 在这个方法里面,我将有一些关于值是’左’还是’右’的if条件。将参数设置为String,显然我可以传递任何字符串值..我想知道它是否可能将Enum值作为方法参数,如果是,如何? 以防有人想到这个; 我想过使用布尔值,但我真的不喜欢它。 首先,如何将真/假与左/右相关联? (好吧,我可以使用注释但我仍然觉得它很脏)其次,我可能会决定添加一个新值,比如’justify’,所以如果我有两个以上的可能值,那么绝对不能使用布尔类型。 有任何想法吗?

编码提示 – 交集类型和java枚举

交集类型允许您(有点排序)执行具有inheritance层次结构的枚举。 您不能inheritance实现,但可以将其委托给辅助类。 enum Foo1 implements Bar {} enum Foo2 implements Bar {} class HelperClass { static <T extends Enum & Bar> void fooBar(T the enum) {} } 当您有许多实现某种模式的不同枚举时,这很有用。 例如,许多具有父子关系的枚举对。 enum PrimaryColor {Red, Green, Blue;} enum PastelColor {Pink, HotPink, Rockmelon, SkyBlue, BabyBlue;} enum TransportMedium {Land, Sea, Air;} enum Vehicle {Car, Truck, BigBoat, LittleBoat, JetFighter, HotAirBaloon;} 您可以编写通用方法,说“好的,给定一个枚举值,该枚举值是某些其他枚举值的父级,子类型的所有可能子枚举中有多少百分比将此特定父值作为其父级?”,并且全部使用类型安全,没有铸造完成。 […]

消除重复的枚举代码

我有大量实现此接口的Enums: /** * Interface for an enumeration, each element of which can be uniquely identified by it’s code */ public interface CodableEnum { /** * Get the element with a particular code * @param code * @return */ public CodableEnum getByCode(String code); /** * Get the code that identifies an element of the enum * […]

如何以原始顺序读取java中的属性文件

我需要读取一个属性文件并在Java中生成一个Properties类。 我这样做是通过使用: Properties props = new Properties(); props.load(new FileInputStream(args[0])); for (Enumeration e = props.propertyNames(); e.hasMoreElements();) { } 但是,props.propertyName返回的属性不是原始属性文件的顺序。 我知道属性只是老式的,非泛化的Hashtables。 我正在寻找一个解决方案。 任何想法? 谢谢!