Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
323 views
in Technique[技术] by (71.8m points)

java enum 变量不能再case里面重复定义?

我需要根据字符串内容对一个变量赋值,但变量类型不一样,我现在是

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

idea报错,重复定义。可是这是枚举,不会重复的。怎么完成我的目标?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

CoviData 和HelloData 可以同时继承父类Data,使用限定的类型通配符申明对象。
List<? extends Data> data。切记要申明在switch之外。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...