Java enum valueOf() with multiple values?

前端 未结 6 791
庸人自扰
庸人自扰 2020-12-07 16:34

I have a problem in Java using Enums. I have read the documentation about assigning value parameters to Enums. But, my question is what about multiple values, is it possible

6条回答
  •  北海茫月
    2020-12-07 17:23

    Alternatively add a method, so your enums and constructor can be more clean if the structure is more complicated:

    public Language getLanguage(String code){
      switch(code){
        case "en":
        case "en_GB":
        ...
        case "eng":
          return ENGLISH;
        case "rus":
        case "russian":
          return RUSSIAN;
      }
    }
    

提交回复
热议问题