Java 协变返回类型不适用于覆盖枚举实例的方法?
我花了很长时间在 Google 上查找有关该主题的一些信息,但与 Java 枚举和协变返回类型相关的结果几乎不存在。
那么:是否可以将协变返回类型与枚举方法一起使用,在枚举类中定义一个方法,然后在实例中重写它,如下所示:
package enumcovariance.test;
public enum CovariantEnum {
INT_INSTANCE(new Integer(3)) {
@Override
public Integer getData () {
return (Integer) super.getData();
}
},
STR_INSTANCE("Hello world") {
@Override
public String getData () {
return (String) super.getData();
}
};
private final Object data;
private CovariantEnum(Object data) {
this.data = data;
}
public Object getData () {
return data;
}
}
然后像这样利用协变:
package enumcovariance.test;
import org.junit.Test;
public class CovariantEnumTest {
@Test
public void intEnumTest () {
Integer i = CovariantEnum.INT_INSTANCE.getData();
}
@Test
public void strEnumTest() {
String s = CovariantEnum.STR_INSTANCE.getData();
}
}
在这种情况下,编译器是我的枚举定义很好,但是测试用例无法编译,说对象无法转换为整数(或字符串)。显然,编译器只查看方法的基本定义,而不查看重写方法。 使用不同的枚举定义,我获得了基本方法抽象,但这仍然不起作用。
我认为在编译过程中枚举的转换方式很复杂,导致它无法工作,但我想确定不仅仅是我在做一些愚蠢的事情。
请注意,这个测试用例确实是非常人为的,在我的实际枚举中这个功能会更有用。如果有需要我可以发帖。
I spent quite a while with Google to find some information on this topic, but results relating to both Java enums and covariant return types were pretty much non-existent.
So: is it possible to use covariant return types with enum methods where you define a method in the enum class and then override it in the instances, like so:
package enumcovariance.test;
public enum CovariantEnum {
INT_INSTANCE(new Integer(3)) {
@Override
public Integer getData () {
return (Integer) super.getData();
}
},
STR_INSTANCE("Hello world") {
@Override
public String getData () {
return (String) super.getData();
}
};
private final Object data;
private CovariantEnum(Object data) {
this.data = data;
}
public Object getData () {
return data;
}
}
And then to take advantage of the covariance like so:
package enumcovariance.test;
import org.junit.Test;
public class CovariantEnumTest {
@Test
public void intEnumTest () {
Integer i = CovariantEnum.INT_INSTANCE.getData();
}
@Test
public void strEnumTest() {
String s = CovariantEnum.STR_INSTANCE.getData();
}
}
In this case the compiler is fine with my enum definition, but the test case fails to compile, saying Object cannot be converted to Integer (or String). Apparently the compiler only looks at the base definition of the method, not the overriding method.
With a different enum definition I had the base method abstract, but that still didn't work.
I'm thinking it's something complex to do with the way enums are transformed during the compile process that prevents it from working, but I want to be sure it's not just me doing something silly.
Note that this test case is admittedly very contrieved, in my actual enum this functionality would be more useful. I can post it if necessary.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CovariantEnum.INT_INSTANCE
的类型是CovariantEnum
,它从getData
返回Object
。不幸的是,您也无法使
enum
类型变得通用。The type of
CovariantEnum.INT_INSTANCE
isCovariantEnum
which returnsObject
fromgetData
.Unfortunately, you can't make the
enum
type generic either.