Why not extending from Enum>

前端 未结 5 698
无人及你
无人及你 2021-01-19 06:36

I stumbled over the following problem that i can\'t extend and implement from this class which is defined in Java 1.5 (java.lang package)

public abstract cla         


        
5条回答
  •  不要未来只要你来
    2021-01-19 06:56

    You can't extend Enum like that. It's built in to the compiler, or the runtime (not sure which).

    But it looks like you're trying to solve the wrong issue; the ordinal of an enum value is not supposed to have any functional meaning. As soon as you give it one, it should have a different name than 'Ordinal'. Your second code snippet is far superior to your first one.

    In general, relying on ordinal for anything is bad practice; it probably never should've been exposed in the first place. The only thing you can rely on is the name, and any value you assign yourself.

    If it's that important to you to name your field 'ordinal', just use the typesafe enum pattern (item 21), which Enum is just an implementation of.

提交回复
热议问题