How to use java.lang.Enum

This is not a question of the new features in the listing. This is not a question that awaits an answer that says enum is equivalent to the xyz extends Enum class. If you want to preach about how enumerations are used, as shown in numerous textbooks, do not answer.

This question is specifically about using the java.lang.Enum class. Rather, it is true that java.lang.Enum cannot be directly extended or created, right? Is it true that java.lang.Enum is only for using an enumeration type and nothing else is right?

If so, the color variables are pretty pointless, aren't they? Even if the compiler resolves this.

final static Enum<colors> colours = null;
enum colors {"R","G","B"};

So that I could equate color variables other than zero.

of course the compiler won't let me do this

class BaseColour extends Enum<BaseColour>{ ,,,}

but he allows us to list an unfair advantage in that he can do this as a manipulator of a manipulator behind the blinds.

I ask this question because I want to do something like these

class Reddish extends BaseColour {"Crimson", "Blood", "Pink"};
class Purplish extends BaseColour {"Violet", "Evening Sky", "Brinjal"};
class Greenish extends BaseColour {"Algae", "Leaf"};

I want to pass any of them, and only any of the types listed are listed in the method

void squeezeColour(BaseColour colourConstraint)
{ ... }

Is it possible? / ** If not, and if you happen to be James Gosling et al, could you please consider this for the next version of the Java specs? ** /

Further addition to the question: I forgot to say this ... I am working on using EnumMap or EnumSet and uses content and range methods. Is that the best I could do?

+3
source share
3 answers

enum, , . , , JVM , - , . enums , .

"type checked enum", , .

class Foo {

  // the constants
  public static final Foo RHUBARB = new Foo();
  public static final Foo CUSTARD = new Foo();

  // only this class and subclasses can create constants
  protected Foo(){ }
}
-2

, , . Google " ", .

- Enum, values ​​() ordinal().

, values ​​() . values ​​() , , :

X instanceof MyEnum = > MyEnum.values ​​() X

Reddish.ordinal() BaseColour.ordinal() . . ( ++), .

+1

You can simply use the dictionary to map the abstract (reddish) look to Enum. That way you saved Enum, and you could compare it using dictionary methods.

+1
source

Source: https://habr.com/ru/post/1724926/


All Articles