枚举常量在Java中实现通用接口 [英] Enum constants implementing generic interfaces in Java

查看:599
本文介绍了枚举常量在Java中实现通用接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你有一个通用界面:

Suppose you have a generic interface:

public interface MyInterface<T> {
    T doSomething();
}

是否可以声明实现 MyInterface< ; T> ,但是哪个每个枚举常量实现它的不同值 T ?那就是这个枚举:

Is it possible to declare an enum that implements MyInterface<T>, but for which which every enum constant implements it for a different value of T? That is, given this enum:

public enum MyEnum {
    FOO,
    BAR,
    BAZ;
}

我们可以更改它,以便 FOO implements MyInterface< Integer> BAR implements MyInterface< String> BAZ 实现 MyInterface< List< MyOtherType>> ,并使其 MyEnum 总体实现 MyInterface<?> ?似乎完全可行的做这个原始的,所以可能是这样可以以类型安全的方式完成。

can we change it so that FOO implements MyInterface<Integer>, BAR implements MyInterface<String>, and BAZ implements MyInterface<List<MyOtherType>>, and make it so that MyEnum overall implements MyInterface<?>? It seems entirely feasible doing this raw, so it may be the case that it can be done in a typesafe manner.

推荐答案

否, amalloy指出,Java不允许使用类型参数声明枚举。清楚的是,如果您考虑使用枚举方式,例如在开关中。

No, as amalloy pointed out, Java doesn't allow enums to be declared with type parameters. It becomes clear why if you think about the way enums are meant to be used, for example in a switch.

还要考虑语言如何实现通用枚举 - 这不是微不足道的。对于通用枚举 MyEnum< T> ,每个枚举常量都需要将 T 解析为某种特定类型,否则根本不会是常数。考虑一下:

Also consider how the language would implement generic enums - it isn't trivial. For a generic enum MyEnum<T>, each enum constant would need to resolve T to some specific type, otherwise they wouldn't be constants at all. Consider this:

enum MyEnum<T> {
    FOO; // T is not resolved
}

什么是 T FOO 在这里?该语言将需要一个新的语法才能表达它,例如:

What is T for FOO here? The language would need a new syntax just to be able to express it, for example:

enum MyEnum<T> {
    FOO<String>;
}

所以现在我们正在为语言增加复杂性,以支持没有过分引人注目的用例的语义。很容易看出,为什么语言设计师只能使用枚举类型参数。

So now we're getting into added complexity for the language in order to support semantics that don't have an overly compelling use case. It's easy to see why the language designers would have simply nixed type parameters for enums.

解决方法:

您只需不使用枚举就可以模拟所需的模式。将您的界面的实现组织到实用程序类中:

You can emulate your desired pattern by simply not using an enum. Organize the implementations of your interface into a utility class:

public class MyImplementations {

    public static final MyInterface<Integer> FOO =
            new MyInterface<Integer>() {
                ...
            };

    public static final MyInterface<String> BAR =
            new MyInterface<String>() {
                ...
            };

    public static final MyInterface<List<MyOtherType>> BAZ =
            new MyInterface<List<MyOtherType>>() {
                ...
            };

    private MyImplementations() { }
}

本质上缺少的东西是一种迭代不同实现的方法,就像您可以使用 MyEnum.values()一样,但假设 MyEnum< T> ,您可以迭代的最具体的类型是 MyEnum<?>

The only thing inherently missing is a way to iterate over the different implementations, as you could have done with MyEnum.values() - but with a hypothetical MyEnum<T>, the most specific type you could iterate over would be MyEnum<?>.

这篇关于枚举常量在Java中实现通用接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆