【JAVA】枚举

package exersice;

import java.util.HashMap;
import java.util.Map;

public enum HeroType {
    TANK("坦克"),
    WIZARD("法师"),
    ASSASSIN("刺客"),
    ASSIST("辅助"),
    WARRIOR("近战"),
    RANGED("远程"),
    PUSH("推进"),
    FARMING("打野");
    
    private String type;
    static Map enumMap= new HashMap<>();
    static {
        for (HeroType heroType : HeroType.values()) {
            enumMap.put(heroType.getChineseName(), heroType);
        }
    }
    
    HeroType(String type) {
        this.type = type;
    }
    
    public static HeroType getHeroType(String chineseName) {
        return enumMap.get(chineseName);
    }
    
    public String getChineseName() {
        return type;
    }
    
    public static void main(String[] args) {
        System.out.println(HeroType.getHeroType("坦克"));
    }
}

你可能感兴趣的:(【JAVA】枚举)