Java泛型的泛型
我有一个代表文本片段的通用类。该文本片段可以具有多种不同模式中的任何一种(不同类型的突出显示)。这些模式由枚举表示。每个项目的枚举可能不同,但它必须实现一个接口,该接口提供了组合其中两个的方法(可以突出显示并加粗)。所以我有一个界面:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
TextFragment
需要说明关于E
的两件事。TextFragmentMode
。Enum
。由于 Java 继承的不稳定,您需要以相反的方式编写:
TextFragment<E>
needs to say two things aboutE
.TextFragmentMode<E>
.Enum<E>
.Because of Java inheritance wonkiness, you need to write that the other way around:
问题是您试图使 E 扩展
TextFragmentMode
和Enum
,它们不是相关类型。什么类型的E
可以满足这两个约束?我怀疑您想要两个类型参数,如下所示:
现在您在不同类型参数上表达了每个约束,并且它们都有意义 - 您绝对可以找到一个
E
它是一个枚举,M
是一个TextFragmentMode
。然而,它相当复杂......你一定需要它是这样通用的吗?您将在课堂上使用
M
做什么?您能否仅将TextFragmentMode
作为构造函数参数(或其他参数)并再次使其在一个类型参数中通用?The problem is that you're trying to make E extend
TextFragmentMode
andEnum
, which aren't related types. What typeE
would satisfy both constraints?I suspect you want two type parameters, something like this:
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 anM
which is aTextFragmentMode<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 aTextFragmentMode<E>
as a constructor parameter (or whatever) and make it generic in one type parameter again?您需要引入一种新类型来解释 Enum 的界限
You need to introduce a new type that accounts for the bound of Enum
未经测试,我猜想:
嗯,简短的测试表明它似乎也不起作用......
Without testing, I'd guess:
Hmm, short test shows it doesn't seem to work either...