Generics of Java generics

I have a generic class that represents a piece of text The text fragment can have any of a variety of different modes (different types of highlighting) These patterns are represented by enumerations The enumeration of each item may be different, but it must implement an interface that provides a way to combine two of them (can be highlighted and bold) 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 of a text string and a pattern But when I try to declare a class:

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

I received the following error:

Where, according to eclipse syntax, highlight refers to

E extends Enum<E>

Partial code Who knows what I did wrong? I must have missed something about generics

——————- Edit --

I finally took the time to read Josh Bloch's "effective Java" in the second edition, and it turned out that he took this use case as item 34: using interfaces to simulate extensible enumerations Just like what I want to say, like great thoughts, it will make too much sense!

Solution

Textfragment & lt e – substitute; Two things need to be said about E

>"Extended" textfragmentmode < E & gt;. > To do this, you must also constrain it to extend enum < E >

Because of the courage of Java inheritance, you need to write in the opposite way:

public class TextFragment<E extends Enum<E> & TextFragmentMode<E>> {
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>