简体   繁体   中英

Why the type parameter in Enum declaration contains recursive type bounds?

  public abstract class Enum<E extends Enum<E>>
            implements Comparable<E>, Serializable {
     // All code goes here

    }

In the code above why there is recursive type bound for the type parameter of the Enum and why is missing for the portion that contains Comparable<E> . In other words why it is Comparable<E> and not Comparable<E extends Comparable<E>>

The Enum<E extends Enum<E>> is defining E and limiting it's type.

By the time you get to Comparable<E> E is now defined so you don't need to further qualify it.

Comparable<<E> extends Comparable<E>> would just be silly. If you mean something like Comparable<E extends Enum<E>> then you wouldn't need it because E has been defined and filtered already. At this point you are now informing the user more about what objects of this class do, ie they implement the Comparable<E> interface.

Enum types have recursive type bounds because they are a recursive type, ie an enum is an Enum of itself (note the case difference).

可能不希望将Comparable的泛型类型参数绑定到特定类型,而Enum必须绑定到扩展Enum的类型。

It is Comparable<E extends Comparable<E>> also. Comparable<E> is identical to Comparable<E extends Enum<E>> cause E extends Enum<E> , Enum<E> is identical to Comparable<E> due to inheritance. Therefore Comparable<E> identical to Comparable<E extends Comparable<E>>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM