using enum in switch/case

前端 未结 2 1398
孤城傲影
孤城傲影 2020-12-30 04:07

I have an entity that has an enum property:

// MyFile.java
public class MyFile {   
    private DownloadStatus downloadStatus;
    // other properties, sette         


        
2条回答
  •  死守一世寂寞
    2020-12-30 04:58

    Every Java enum has an ordinal which is automatically assigned, so you don't need to manually specify the int (but be aware that ordinals start from 0, not 1).

    Then, to get your enum from the ordinal, you can do:

    int downloadStatus = ...
    DownloadStatus ds = DownloadStatus.values()[downloadStatus];
    

    ... then you can do your switch using the enum ...

    switch (ds)
    {
      case NOT_DOWNLOADED:
      ...
    }
    

提交回复
热议问题