我需要创建一个需要表示以下内容的结构(对于类别和子类别)。它只有一层深。我正在考虑使用 Java Enums 来做这件事,但不确定如何表示这种层次结构。
我的代表设备的 java 对象(业务对象)将具有类别和子类别属性,我想使用枚举而不是使用整数代码,如 100、1 等。一些设备将只有类别而不是子类别类别(如以下示例中的 300)。
100 Switch
1 Interior
2 Exterior
200 Security Sensor
1 Door Sensor
2 Leak Sensor
3 Motion Sensor
300 Camera
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏。
谢谢
这篇java.dzone文章展示了分层枚举的一个漂亮示例:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
这篇文章展示了您可以通过此设置实现的许多小技巧。
and*_*ndy -1
下面的代码能满足您的要求吗?
public enum Category {Switch, ...}
public enum SubCategory {Interior, ...}
Run Code Online (Sandbox Code Playgroud)