Java Enum return Int

前端 未结 8 2035
小鲜肉
小鲜肉 2020-12-13 03:31

I\'m having trouble declaring an enum. What I\'m trying to create is an enum for a \'DownloadType\', where there are 3 download types (AUDIO, VIDEO, AUDIO_AND_VIDEO).

<
8条回答
  •  难免孤独
    2020-12-13 04:00

    If you are concatenating the enum with a string you can override toString method to return the int:

    public String toString() {
        return value + "";
    }
    

    Then you could simply use:

    String something = "foo" + DownloadType.AUDIO;
    

    and the toString() method will be invoked.


    Note that using toString() programmatically is generally considered poor practice - it is intended for human eyes only, however this is the only way to achieve what you're asking.

提交回复
热议问题