0

我的对象:

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

此类事情的最佳做法是什么?

4

1 回答 1

1

像这样更改您的枚举类;

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

于 2012-09-21T03:43:41.840 回答