我怎样才能检查枚举类型的值是否相等?我尝试了以下方法,但它失败了。为什么?
class enum Test {
LETTER("1A");
private String value;
public Test(String value) {
this.value = value;
}
@Override
public void toString() {
return value;
}
}
String teststring = "1A".
Sysout(teststring.equals(Test.LETTER)); //false. Why???
of
函数可能会出现潜在问题。如果您有LETTER_B("1A"), LETTER("1A");
作为枚举常量,则Test.of(teststring) == Test.LETTER)
将返回false
。(即使这些枚举常量实际上很愚蠢,因为它们基本上是相同的) - Alexis C.==
比较String
。但这是一个不同的问题,一个返回单个对象的方法无法处理两个对象匹配条件的情况。 - Holger