在这种情况下使用适合的嵌套枚举? [英] Use of Nested enums appropriate in this case?

查看:180
本文介绍了在这种情况下使用适合的嵌套枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要支持一些 ChartTypes 。这些图表类型中的每一种都可以支持许多 ChartSubTypes 。例如 AreaChart 类型可以具有 PercentArea StackedArea 等我正在考虑在 ChartTypes SubTypes 中使用枚举,然后在某处保持一个地图,如下所示:

I have a requirement to support a number of ChartTypes. Each of these chart types can support a number of ChartSubTypes. For example AreaChart type can have PercentArea, StackedArea etc. I am thinking of using an Enum both for ChartTypes and SubTypes and then maintain a map somewhere which will be something like :

Map<ChartType,List<ChartSubTypes> mapTypes;

我可以以某种方式在这里使用嵌套的枚举模式吗?如果是,那么如何?

Can I somehow use a nested enum pattern here? If yes then how?

推荐答案

如果该定义是不变的(即您知道哪些子类型可以包含每种类型)可以使用这里的枚举定义如下

If that definition is constant (i.e. You know which sub types can contain every type) You can use here enum definitions as follows

enum ChartSubTypes{
    PercentArea, StackedArea, ChartSubType3;
}

enum ChartTypes{
    AreaChart(ChartSubTypes.PercentArea, ChartSubTypes.StackedArea), 
    CharType2(ChartSubTypes.PercentArea, ChartSubTypes.ChartSubType3);

    private List<ChartSubTypes> subTypes = new ArrayList<ChartSubTypes>();

    private ChartTypes(ChartSubTypes ...chartSubTypes){
        for(ChartSubTypes subType : chartSubTypes){
            subTypes.add(subType);
        }
    }

    public List<ChartSubTypes> getSubTypes(){
        return Collections.unmodifiableList(subTypes);
    }
   }

这篇关于在这种情况下使用适合的嵌套枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆