0

如何使用 Java 支持树状类型的数据结构Enumeration

例如Type,它subTypes和它们subTypes合而为一Enum

这将允许只迭代代码中的类型并派生其父级等,以检查Type树中特定类型的起源和位置。

4

1 回答 1

0

经过数小时的搜索,我在这个很棒的链接中找到了答案

枚举层次结构摘自下面的链接

 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;
    }
}

这可以通过为查找提供值来进一步扩展

喜欢

 public enum OsType {
    OS(null,null),
        Windows(OS,"Win"),
            WindowsNT(Windows,"WinNT"),
                WindowsNTWorkstation(WindowsNT,"WinNTWorkStation")
....
private OsType parent = null;
private String desc = null;

private OsType(OsType parent,String desc) {
    this.parent = parent;
    this.desc   = desc;
}
于 2013-08-08T16:29:30.357 回答