Home > Back-end >  The basic knowledge of Java, has been enumerated this thing don't understand.
The basic knowledge of Java, has been enumerated this thing don't understand.

Time:12-07

The UP DOWN LEFT RIGHT is a variable? So, what data type is?
Write a word casually add a comma, a new line look is neither a basic data types, also is not a class object,
Direction. The UP is a kind of tag? Tags have to be a basic data types, or object type?

 
Public enum Direction {
UP,
DOWN,
LEFT,
RIGHT;
}

CodePudding user response:

Members of the enumeration is equal to the enumeration object instances of a class, suggest to look at this article https://blog.csdn.net/qq_35385687/article/details/90147104

CodePudding user response:

Enumerated the compiled too. The class files, I think you can put the enumeration name as the name of the class, put inside as a constant, so good to understand a bit, the data type is an enumeration name

CodePudding user response:

Enumeration type equivalent to a static instance of a class
For example,
Class A {
Public static UP=new A (1, "UP");
Public static DOWN=new A (2, "DOWN");

Private int index;
Private String value.
Private A (int index, String value) {
Enclosing the index=index;
This value=https://bbs.csdn.net/topics/value;
}
}
LZ can see Enum interface and instructions on the Enum class

CodePudding user response:

refer to the second floor BioIT response:
enumeration compiled too. The class files, I think you can see the enumeration name as the name of the class, put inside as a constant, so good to understand a bit, the data type is an enumeration name

But these constants can neither store Numbers or characters, and cannot store object,

CodePudding user response:

Reference 3 l, also has a constructor enumeration, don't say village Numbers or characters, objects are not surprising, oneself Google baidu enumeration method to construct learning how to learn more

CodePudding user response:

Ha ha?? ! I want to say a lot, and prices are flushed bright red? We should strive to

CodePudding user response:

Enumeration is to express a variety of static constants, use the special syntax to build constants, just, enumerated expression effect is better, the content of the description is also more abundant,

CodePudding user response:

reference 4 floor qq_16774199 response:
Quote: refer to the second floor BioIT response:
enumeration compiled too. The class files, I think you can put the enumeration name as the name of the class, put inside as a constant, so good to understand a bit, the data type is an enumeration name

But these constants can neither store Numbers or characters, and cannot store object,

You should just look at a simple overview of the enumeration, recommend this book to see the Java core technology, to speak more full
Enumerated types can save any of you would like to deposit your

Public enum Direction {
The UP (" UP "),
DOWN (" DOWN "),
The LEFT (" LEFT "),
RIGHT (" RIGHT ");
Private String a;
Private Direction (String a) {
Enclosing a=a;
}
Public String getA () {
Return a;
}
}

UP (" UP ") is equivalent to an enumeration type constructor
This code through simple changes have saved up a String object
Then you can through the
Direction [] directions=Direction. The values ()
To obtain contains the UP, DOWN, LEFT, RIGHT the four examples of array
Through the directions again [0]. GetA () can get UP the actual String object in the

CodePudding user response:

reference 4 floor qq_16774199 response:
Quote: refer to the second floor BioIT response:
enumeration compiled too. The class files, I think you can put the enumeration name as the name of the class, put inside as a constant, so good to understand a bit, the data type is an enumeration name

But these constants can neither store Numbers or characters, and cannot store object,

You should just look at a simple overview of the enumeration, recommend this book to see the Java core technology, to speak more full
Enumerated types can save any of you would like to deposit your

Public enum Direction {
The UP (" UP "),
DOWN (" DOWN "),
The LEFT (" LEFT "),
RIGHT (" RIGHT ");
Private String a;
Private Direction (String a) {
Enclosing a=a;
}
Public String getA () {
Return a;
}
}

UP (" UP ") is equivalent to an enumeration type constructor
This code through simple changes have saved up a String object
Then you can through the
Direction [] directions=Direction. The values ()
To obtain contains the UP, DOWN, LEFT, RIGHT the four examples of array
Through the directions again [0]. GetA () can get UP the actual String object in the

CodePudding user response:

reference qybao reply: 3/f
enumerated type equivalent to a static instance of a class
For example,
Class A {
Public static UP=new A (1, "UP");
Public static DOWN=new A (2, "DOWN");

Private int index;
Private String value.
Private A (int index, String value) {
Enclosing the index=index;
This value=https://bbs.csdn.net/topics/value;
}
}
LZ can see Enum interface and the specification of an Enum


+ 1
Before jdk1.5, does not support an enumerated type, basically is to use this way to simulate the enumeration

CodePudding user response:

Enumeration is a type, it can serve as a class member variable, or a method, local variables with the code, this and general class, such as String, ArrayList, such as the Date
no essential difference betweenThe difference is that the enumeration values can be specified in the enumeration of that a few, or an empty value

CodePudding user response:

This is when I practice writing an enumeration, generally work also is this kind of style to write, the enumeration is mainly used to check some type, corresponding to the feeling of some configuration
 public enum Sex {
NAN (1, "male"),
NV (2, "female"),
WQD (0, "not sure");
Private final int sex;
Private final String name;
Sex (int Sex, String name) {
this.sex=sex;
this.name=name;
}
Public String getName () {
return name;
}
}

CodePudding user response:

Enumerated types (enum), don't repeat the data is stored as a set of predefined,
Sometimes can use enum instead of the commonly used string type,
Enum storage is very compact, will list values down to one or two bytes,
Enum in internal storage, actually save are integers,
Try to avoid using digital as enum enumerated constants, because of easy to chaos,
An integer sorting is according to the internal storage
  • Related