如何将全局枚举呈现为多个类的子类?
我有一个静态最终 Java 类库,它描述了一系列标准。这些类(简化)之一的 night 看起来像:
public static final class ResponseControl {
public static enum EventStatus {
EVENT_RECEIVED,
EVENT_STARTED,
EVENT_COMPLETED;
}
}
将枚举 EventStatusOption 作为 ResponseControl 类的子类包含起来很方便,因为这是它在标准文档中定义的地方,也是人们寻找它的地方。它允许他们这样说:
if (respCtl.status == ResponseControl.EventStatus.EVENT_STARTED) { ... }
但我实际上有数百个这样的类,并且在某些情况下,多个类中使用相同的枚举。能够在其他地方定义枚举,然后做这样的事情会很好:
public static enum GlobalEventStatus {
EVENT_RECEIVED,
EVENT_STARTED,
EVENT_COMPLETED;
}
public static final class ResponseControl {
public static enum EventStatus extends GlobalEventStatus {}
public final EventStatus status;
}
if (respCtl.status == ResponseControl.EventStatus.EVENT_STARTED) { ... }
但是,我不能这样做,因为扩展枚举是不合法的,即使(如我的情况)我不这样做我真的不想扩展它们,我只是想让全局类型在本地类中可用。有谁知道有什么方法可以实现这一目标?
下面 ColinD 答案的第二段实际上是我需要的答案。通过说:
public interface GlobalEvent
{
public static enum Status {
EVENT_RECEIVED,
EVENT_STARTED,
EVENT_COMPLETED;
}
}
public static final class ResponseControl {
public static enum Event implements GlobalEvent
public final Event.Status status;
}
我可以允许我的用户说:
if (respCtl.status == ResponseControl.Event.Status.EVENT_STARTED) { ... }
......同时仍然在一个地方保留实际定义。谢谢。
I have a library of static final Java classes which describes a body of standards. One of these classes (simplified) night look like:
public static final class ResponseControl {
public static enum EventStatus {
EVENT_RECEIVED,
EVENT_STARTED,
EVENT_COMPLETED;
}
}
It's convenient to include the enum EventStatusOption as a subclass of the class ResponseControl, because that's where it's defined in the standards document, and that's where people will be looking for it. It allows them to say things like:
if (respCtl.status == ResponseControl.EventStatus.EVENT_STARTED) { ... }
But I have literally hundreds of these classes, and in some cases the same enum is used in multiple classes. It would be nice to be able to define the enum somewhere else, and then do something like this:
public static enum GlobalEventStatus {
EVENT_RECEIVED,
EVENT_STARTED,
EVENT_COMPLETED;
}
public static final class ResponseControl {
public static enum EventStatus extends GlobalEventStatus {}
public final EventStatus status;
}
if (respCtl.status == ResponseControl.EventStatus.EVENT_STARTED) { ... }
I can't do this, however, because it isn't legal to extend enums, even (as in my case) where I don't really want to extend them at all, I just want to make the global type available in the local class. Does anyone know of a way to accomplish this?
The second paragraph of ColinD's answer below is actually the answer I needed. By saying:
public interface GlobalEvent
{
public static enum Status {
EVENT_RECEIVED,
EVENT_STARTED,
EVENT_COMPLETED;
}
}
public static final class ResponseControl {
public static enum Event implements GlobalEvent
public final Event.Status status;
}
I can allow my users to say:
if (respCtl.status == ResponseControl.Event.Status.EVENT_STARTED) { ... }
...all while still maintaining the actual definitions in a single place. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需使用名为
EventStatus
的常见顶级类型即可。这很正常,我认为任何 Java 程序员都不会觉得它令人困惑……你想要做的事情听起来更令人困惑。如果您要寻找的是一组通用的枚举常量,然后是定义特定于某个类的附加常量的子类……那么,您就很不走运了。你不能用枚举来做到这一点。一种选择(并不是说这是最好的)是使
EventStatus
成为一个接口,然后让GlobalEventStatus
成为实现该接口的枚举。然后,您可以有另一个枚举ResponseControl.Status
或一些也实现EventStatus
的枚举。我还应该提到像 ResponseControl.EventStatus 这样的东西不是“子类”而是“嵌套类”。
Just use a common top-level type called
EventStatus
. That's normal and I don't think any Java programmer will find it confusing at all... what you're trying to do sounds more confusing.If what you're looking for is a common set of enum constants and then subclasses that define additional constants specific to a certain class... well, you're pretty much out of luck there. You can't do that with enums. One option (not saying it's the best) would be to make
EventStatus
an interface and then haveGlobalEventStatus
be an enum that implements that interface. You could then have another enumResponseControl.Status
or some such that also implementsEventStatus
.I should also mention that something like
ResponseControl.EventStatus
is not a "subclass" but a "nested class".