Enum enumeration class
① Differences between enumeration classes and common classes:
The enumeration class defined by Enum inherits the java. Lang. Enum class by default.
The constructor of the enumeration class can only use the private access controller.
All instances of the enumeration class must be explicitly listed (, separated; ended) in the enumeration class. The listed instance system will automatically add public static final Modification
② In JDK 1.5, you can use the enum-defined enumerated class object in the switch expression as the expression,
The case clause can directly use the name of an enumeration value without the need to add an enumeration class as a limitation.
③ Main methods of enumeration classes:
Values () method: returns an array of enumerated objects. This method can easily traverse all enumeration values.
Valueof (string Str): You can convert a string into an enumerated class object. The string must be the "name" of the enumerated class object ".
If not, a running exception occurs.
Annotation
① JDK built-in basic annotation types (3)
@ Override: Specifies to override the parent class method. This comment can only be used for methods.
@ Deprecated: indicates that a program element (class, method, etc.) has expired.
@ Suppresswarnings: suppress compiler warnings
② Custom annotation type
Define a new annotation type using the @ interface keyword
The annotation member variables are declared in the form of a non-parameter method in the annotation definition. The method name and return value define the name and type of the member.
You can specify the initial value when defining the annotation member variable. You can use the default keyword to specify the initial value of the member variable.
Public @ interface myannotation {
String name () Default "wzg ";
}
An Annotation without a member definition is called a tag; An Annotation with a member variable is called a Metadata Annotation.
③ Annotations (4)
@ Retention: Only one annotation definition can be modified to specify how long the annotation can be retained,
@ Resolve ention contains a retentionpolicy member variable. When @ resolve ention is used, you must specify a value for this value member variable:
Retentionpolicy. Source: the compiler directly discards the comment of this policy.
Retentionpolicy. Class: the compiler records comments in the class file. When a Java program is run, the JVM does not retain the annotations. This is the default value.
Retentionpolicy. runtime: the compiler records the annotation in the class file. When a Java program is running, the JVM retains the annotation. The program can obtain the annotation through reflection.
@ Target: modifies the annotation definition to specify which program elements can be modified by the modified annotation. @ target also contains a member variable named value.
@ Incluented: specifies that the annotation class modified by the meta-annotation will be extracted as a document by the javadoc tool.
The retention value must be set to runtime for the annotation defined as incluented.
@ Inherited: The annotation modified by it will be inherited. If a class uses the annotation modified by @ inherited, its subclass will automatically have this Annotation
In practical applications, use less
Obtain annotation information using reflection (involved in reflection)
Zookeeper
Enum enumeration class | annotation Annotation