如何使用 Java 支持树状类型的数据结构Enumeration
例如Type
,它subTypes
和它们subTypes
合而为一Enum
这将允许只迭代代码中的类型并派生其父级等,以检查Type
树中特定类型的起源和位置。
经过数小时的搜索,我在这个很棒的链接中找到了答案
枚举层次结构摘自下面的链接
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;
}