java Java泛型的泛型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4248223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 05:30:55  来源:igfitidea点击:

Java generics of generics of

javagenericsenums

提问by Lucas

I have a generic class which represents a fragment of text. That fragment of text may have any of a number of different modes (different types of highlighting). Those modes are represented by an Enum. The Enum could be different for each project but it must implement an interface which provides a method to combine 2 of them (could be highlighted and bolded). So i have an interface:

我有一个表示文本片段的通用类。该文本片段可能具有多种不同模式(不同类型的突出显示)中的任何一种。这些模式由枚举表示。每个项目的 Enum 可能不同,但它必须实现一个接口,该接口提供组合其中 2 个的方法(可以突出显示和加粗)。所以我有一个界面:

public interface TextFragmentMode<E extends Enum<E>> {
    /**
     * Will combine the supplied mode with the current mode and return the
     * result.
     * 
     * @param mode The mode to combine with.
     * @return The combined mode.
     */
    public E combine( E mode );
}

Then my TextFragment is a container for both a String of text, and a mode. But when I try to declare the class:

然后我的 TextFragment 是文本字符串和模式的容器。但是当我尝试声明类时:

public class TextFragment<E extends TextFragmentMode<E extends Enum<E>>> {
    StringBuilder text;
    E mode;
    ...

I get the following error:

我收到以下错误:

Syntax error on token "extends", , expected

令牌“扩展”的语法错误,预期

Which, according to eclipse syntax highlighting, is referring to the

其中,根据 eclipse 语法高亮显示,指的是

E extends Enum<E>

portion of the code. Does anyone know what I am doing wrong? I must be missing something about Generics...

代码的一部分。有谁知道我做错了什么?我一定遗漏了一些关于泛型的东西......

--------------------- edit -------------------

- - - - - - - - - - - 编辑 - - - - - - - - - -

I'm finally taking the time to read Effective Java by Josh Bloch (second edition), and it turns out he goes over this use case as Item 34: Emulate extensible enums with interfaces. As much as I would like to say great mind think alike... That would be WAY too presumtuous!

我终于花时间阅读了 Josh Bloch 的 Effective Java(第二版),结果他将这个用例作为条款34:Emulate extensible enums with interfaces。尽管我想说伟大的思想都一样……那太冒昧了!

回答by Tom Hawtin - tackline

TextFragment<E>needs to say two things about E.

TextFragment<E>需要说两件事E

  • It "extends" TextFragmentMode<E>.
  • In order to do that, you must also constrain it to extend Enum<E>.
  • 它“延伸” TextFragmentMode<E>
  • 为此,您还必须将其限制为扩展Enum<E>.

Because of Java inheritance wonkiness, you need to write that the other way around:

由于 Java 继承的不稳定性,您需要反过来写:

public class TextFragment<E extends Enum<E> & TextFragmentMode<E>> {

回答by Jon Skeet

The problem is that you're trying to make E extend TextFragmentModeandEnum, which aren't related types. What type Ewould satisfy both constraints?

问题是你试图让 E 扩展TextFragmentModeEnum,它们不是相关类型。哪种类型E会同时满足这两个约束?

I suspect you want twotype parameters, something like this:

我怀疑您需要两个类型参数,如下所示:

public class TextFragment<E extends Enum<E>, M extends TextFragmentMode<E>>

Now you have each constraint expressed on a different type parameter, and they both make sense - you can definitely find an Ewhich is an enum, and an Mwhich is a TextFragmentMode<E>. However, it's pretty complicated...

现在,您在不同的类型参数上表达了每个约束,并且它们都有意义 - 您绝对可以找到 a Ewhich 是枚举,而Mwhich 是 a TextFragmentMode<E>。然而,这很复杂......

... do you definitelyneed it to be this generic? What will you be doing with Min the class? Could you not just take a TextFragmentMode<E>as a constructor parameter (or whatever) and make it generic in one type parameter again?

...你肯定需要它是通用的吗?你会M在课堂上做什么?你能不能不只是把 aTextFragmentMode<E>作为构造函数参数(或其他)并再次使它成为一个类型参数的泛型参数?

回答by John Vint

You need to introduce a new type that accounts for the bound of Enum

你需要引入一个新的类型来解释 Enum 的边界

public class TextFragment<T extends Enum<T>, E extends TextFragmentMode<T>> {

回答by Puce

Without testing, I'd guess:

没有测试,我猜:

public class TextFragment<E extends TextFragmentMode<E>> {

Hmm, short test shows it doesn't seem to work either...

嗯,简短的测试表明它似乎也不起作用......