Home  >  Q&A  >  body text

Java enum variables cannot be defined repeatedly in cases?

I need to assign a value to a variable based on the string content, but the variable type is different. I am now

        switch (weatherENUM){
            case weather:
               List<CoviData> data =  weathreService.getCoviData(reportType);
                break;
            case COVI:
                List<helloData> data =  weathreService.gethelloData(reportType);
        }

Idea reported an error and repeated the definition. But this is an enumeration and will not be repeated. How do I accomplish my goal?

黄舟黄舟2712 days ago862

reply all(1)I'll reply

  • 大家讲道理

    大家讲道理2017-05-17 10:11:15

    CoviData and HelloData can inherit the parent class Data at the same time, and use qualified type wildcards to declare objects.
    List<? extends Data> data. Remember to declare it outside of switch.

    reply
    0
  • Cancelreply