如何在java中从枚举中获取名称
我在java中有一个enum类,如下所示如何在java中从枚举中获取名称,java,enums,Java,Enums,我在java中有一个enum类,如下所示 public enum SMethod { /** * LEAVE IN THIS ORDER */ A (true, true, true,false), B (true, true, false,false), C (true, true, false,false), D (false, false, false) } 另一类有下面的方法 private String getSMethod(boolean isSd
public enum SMethod {
/**
* LEAVE IN THIS ORDER
*/
A (true, true, true,false),
B (true, true, false,false),
C (true, true, false,false),
D (false, false, false)
}
另一类有下面的方法
private String getSMethod(boolean isSds) {
if (isClsSds)
return "A";
else
return "B";
}
当前此方法返回的是硬代码值,但不是字符串。但我想使用SMethod enum返回它。我编写了以下代码:
private SMethod getSMethod(boolean isSds) {
if (isClsSds)
return SMethod.A;
else
return SMethod.B;
}
但是我需要的是这个方法应该返回字符串。使用方法:
要获取枚举对象的字符串
名称。请使用以下方法:
return SMethod.A.name(); will return string
获取枚举对象的字符串
名称
return SMethod.A.name(); will return string
见方法
返回此枚举常量的名称,与在其枚举声明中声明的名称完全相同
见方法
返回此枚举常量的名称,与在其枚举声明中声明的名称完全相同
有两种方法
public String getName(SMethod enm)
{
return enm.name();
// or enm.toString();
}
有两种方法
public String getName(SMethod enm)
{
return enm.name();
// or enm.toString();
}
SMethod.A.name()应该为您提供字符串。SMethod.A.name()应该为您提供字符串。