Java Enum definition

后端 未结 7 646
孤独总比滥情好
孤独总比滥情好 2020-11-22 17:24

I thought I understood Java generics pretty well, but then I came across the following in java.lang.Enum:

class Enum>
         


        
7条回答
  •  無奈伤痛
    2020-11-22 17:46

    The following is a modified version of the explanation from the book Java Generics and Collections: We have an Enum declared

    enum Season { WINTER, SPRING, SUMMER, FALL }
    

    which will be expanded to a class

    final class Season extends ...
    

    where ... is to be the somehow-parameterised base class for Enums. Let's work out what that has to be. Well, one of the requirements for Season is that it should implement Comparable. So we're going to need

    Season extends ... implements Comparable
    

    What could you use for ... that would allow this to work? Given that it has to be a parameterisation of Enum, the only choice is Enum, so that you can have:

    Season extends Enum
    Enum implements Comparable
    

    So Enum is parameterised on types like Season. Abstract from Season and you get that the parameter of Enum is any type that satisfies

     E extends Enum
    

    Maurice Naftalin (co-author, Java Generics and Collections)

提交回复
热议问题