Java泛型的泛型

发布于 2024-10-03 23:57:16 字数 1032 浏览 2 评论 0原文

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

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 );
}

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

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

我收到以下错误:

令牌“extends”上的语法错误,预期

根据 Eclipse 语法突出显示,这是指

E extends Enum<E>

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

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

我终于花时间阅读 Josh Bloch 所著的《Effective Java》(第二版),事实证明他将这个用例作为项目 34:使用接口模拟可扩展枚举。尽管我想说英雄所见略同……但那未免太自以为是了!

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:

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:

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

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

蓝梦月影 2024-10-10 23:57:16

TextFragment 需要说明关于 E 的两件事。

  • 它“扩展”了TextFragmentMode
  • 为此,您还必须限制它扩展 Enum

由于 Java 继承的不稳定,您需要以相反的方式编写:

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

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

  • It "extends" TextFragmentMode<E>.
  • In order to do that, you must also constrain it to extend Enum<E>.

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

public class TextFragment<E extends Enum<E> & TextFragmentMode<E>> {
傻比既视感 2024-10-10 23:57:16

问题是您试图使 E 扩展 TextFragmentMode Enum,它们不是相关类型。什么类型的 E 可以满足这两个约束?

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

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

现在您在不同类型参数上表达了每个约束,并且它们都有意义 - 您绝对可以找到一个E它是一个枚举,M 是一个 TextFragmentMode。然而,它相当复杂......

一定需要它是这样通用的吗?您将在课堂上使用 M 做什么?您能否仅将 TextFragmentMode 作为构造函数参数(或其他参数)并再次使其在一个类型参数中通用?

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

I suspect you want two type 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 E which is an enum, and an M which is a TextFragmentMode<E>. However, it's pretty complicated...

... do you definitely need it to be this generic? What will you be doing with M in 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?

仙女山的月亮 2024-10-10 23:57:16

您需要引入一种新类型来解释 Enum 的界限

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

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

public class TextFragment<T extends Enum<T>, E extends TextFragmentMode<T>> {
幽梦紫曦~ 2024-10-10 23:57:16

未经测试,我猜想:

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

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

Without testing, I'd guess:

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

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

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