我的对象:
public class Account(){
private String accountName;
private AccountType accountType; // enum
//I customized the getter by doing this...
public String getAccountType(){
return accountType.getAccountType();
}
}
我的AccountType枚举:
public enum AccountType{
OLD("Old account"),
NEW("New account");
private final String accountType;
private AccountType(String accountType){
this.accountType = accountType;
}
public String getAccountType(){
return accountType;
}
}
我使用
${account.accountType}
来检索枚举常量的值。这是正确的方法吗?我尝试使用
AccountType.valueOf("OLD")
,但返回了OLD
。这些事情的最佳实践是什么?
最佳答案
像这样更改您的枚举类;
public enum AccountType{
OLD {
public String type() {
return "Old account";
}
},
NEW {
public String type() {
return "New account";
}
};
}
和您的Account对象是这样的;
public class Account(){
private String accountName;
private AccountType accountType; // enum
//You don't need this.
//public String getAccountType(){
// return accountType.getAccountType();
// }
}
然后您可以访问
accountType.type