How to build an hierarchy tree of categories in java using enums or any other way?

前端 未结 3 1897
借酒劲吻你
借酒劲吻你 2021-01-03 10:35

Assuming that we have a set of categories: categories={A,B}. Let\'s assume more that A consists of subcategories: {A1,A2,A3} and B consists of subcategories: {B1,B2}.In addi

3条回答
  •  有刺的猬
    2021-01-03 11:02

    In addition to the answers above, I would like to share something I found on the internet too. I have not tested it yet, but it seems to offer an alternative:

    http://alexradzin.blogspot.hk/2010/10/hierarchical-structures-with-java-enums_05.html

    public enum OsType {
    OS(null),
        Windows(OS),
            WindowsNT(Windows),
                WindowsNTWorkstation(WindowsNT),
                WindowsNTServer(WindowsNT),
            Windows2000(Windows),
                Windows2000Server(Windows2000),
                Windows2000Workstation(Windows2000),
            WindowsXp(Windows),
            WindowsVista(Windows),
            Windows7(Windows),
            Windows95(Windows),
            Windows98(Windows),
        Unix(OS) {
                @Override
                public boolean supportsXWindows() {
                    return true;
                }
            },
            Linux(Unix),
            AIX(Unix),
            HpUx(Unix),
            SunOs(Unix),
    ;
    private OsType parent = null;
    
    private OsType(OsType parent) {
        this.parent = parent;
    }
    

提交回复
热议问题