How to define properties for Enum items

前端 未结 2 850
执笔经年
执笔经年 2020-12-07 23:50

I have read the question Difference of Enum between java and C++? but I\'m still confused.

I would like the following to return the related String:

p         


        
2条回答
  •  悲哀的现实
    2020-12-08 00:50

    If the pattern holds, this works as well and eliminates the repetition:

    public enum Checker {
        EMPTY,
        RED,
        YELLOW;
    
    
        public String getDescription(){
            String name = name();
            return ""+Character.toUpperCase(name.charAt(0))
                     +name.substring(1).toLowerCase();
        }
    
    }
    

提交回复
热议问题