2013-04-22 66 views
1

取的名字我有一个枚举类在Java中如下如何从枚举在Java

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枚举来返回它。我写它如下:

private SMethod getSMethod(boolean isSds) { 
    if (isClsSds) 
     return SMethod.A; 
    else 
     return SMethod.B; 
} 

但我的需要是这种方法应该retu String。

+0

SMethod.A.name()应该给你的字符串。 – 2013-04-22 07:43:17

回答

2

使用name()方法:

return SMethod.A.name(); 

要获得枚举对象的String名称。

2
return SMethod.A.name(); will return string 

看到name()方法

返回此枚举常量的名称,正是因为在枚举声明中宣布。

1

有两种方法。

所以,你可以使用

public String getName(SMethod enm) 
{ 
    return enm.name(); 
    // or enm.toString(); 
}