Java 协变返回类型不适用于覆盖枚举实例的方法?

发布于 2024-10-11 07:44:54 字数 1257 浏览 0 评论 0原文

我花了很长时间在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自此以后,行同陌路 2024-10-18 07:44:54

CovariantEnum.INT_INSTANCE 的类型是 CovariantEnum,它从 getData 返回 Object

不幸的是,您也无法使 enum 类型变得通用。

The type of CovariantEnum.INT_INSTANCE is CovariantEnum which returns Object from getData.

Unfortunately, you can't make the enum type generic either.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文