Enumerate classes How to customize enum classes
Custom enumeration classes required before JDK1.5
JDK 1.5 New enum keyword is used to define enum classes
If the enumeration has only one member, it can be implemented as a singleton pattern
Enumerates classes Class SEASON{//1. Provides the properties of the class, declared private final private final string seasonname;private final string seasondesc;//2. A property declared as final, initialized in the constructor. Private Season (String seasonname,string seasondesc) {this.seasonname = Seasonname;this.seasondesc = Seasondesc;} 3. Call the property public through the common Method String Getseasonname () {return seasonname;} Public String Getseasondesc () {return seasondesc;} 4. Create an object for the enumeration class: Declare the object of the class public static finalpublic static final Season spring = new Season ("Spring", "Spring Blossoms");p ublic static FINA L Season SUMMER = new Season ("SUMMER", "Summer scorching");p ublic static final Season AUTUMN = new Season ("AUTUMN", "crisp");p ublic STA Tic final Season WINTER = new Season ("WINTER", "Snowy"); @Overridepublic String toString () {return ' Season [seasonname= ' + S Easonname + ", seasondesc=" + Seasondesc + "]";} public void Show () {System.out.println ("This is a Season");}}
Enum Enum class
The enumeration class object must be declared on the first line of the enumeration class.
The difference between an enumeration class and a normal class:
Enum classes defined with enum inherit the Java.lang.Enum class by default
The constructor for an enumeration class can only use the private access control character
All instances of an enumeration class must be explicitly listed (, delimited; end) in an enumeration class. The listed instance system automatically adds the public static final decoration
Interface info{void Show ();} Enum class enum Season1 implements Info{spring ("Spring", "Spring") {public void Show () {System.out.println ("Where is Spring?") }},summer ("SUMMER", "summer scorching") {public void Show () {System.out.println ("Live Like Summer Flowers"),}},autumn ("AUTUMN", "Autumn") {public void Show () {System.out.println ("Autumn is used to break up the season"),}},winter ("WINTER", "Snowy") {public void Show () {System.out.println (" A fire in the Winter ");}; Private final String Seasonname;private final string seasondesc;private Season1 (String seasonname,string seasondesc) { This.seasonname = Seasonname;this.seasondesc = Seasondesc;} Public String Getseasonname () {return seasonname;} Public String Getseasondesc () {return seasondesc;} @Overridepublic String toString () {return "Season [seasonname=" + Seasonname + ", seasondesc=" + Seasondesc + "]";}}
The primary method for enumerating classes:
Values () Method: Returns an array of objects of the enumerated type. This method makes it easy to iterate through all the enumeration values.
ValueOf (String str): A string can be converted to the corresponding enumeration class object. The requirement string must be the "first name" of the enumeration class object. If not, there will be a run-time exception.
How to enable an enumeration class to implement an interface: You can have objects of different enum classes invoke overridden abstract methods, performing different effects. (equivalent to having each object override an abstract method)
Note annotation
Annotation is actually a special tag in the code that can be read at compile, class load, run time, and perform the appropriate processing. By using Annotation, programmers can embed some supplemental information in the source file without changing the original logic.
The basic Annotation
@Override: Qualifies overriding the parent class method, which can only be used for methods
@Deprecated: Used to indicate that a program element (class, method, and so on) is obsolete
@SuppressWarnings: Suppress Compiler warnings
Custom Annotation
Define a new Annotation type using @interface keyword
The member variables of the Annotation are declared in the Annotation definition in the form of a parameterless method. Its method name and return value define the name and type of the member.
You can specify an initial value for an ANNOTATION member variable when you define it, specifying the initial value of the member variable to use the default keyword
Public @interface myannotation{ String name () default "Atguigu";}
A Annotation without a member definition is called a tag; A Annotation that contains member variables is called metadata Annotation
Meta annotations
The meta-Annotation of the JDK is used to decorate other Annotation definitions
@Retention: can only be used to decorate a Annotation definition that specifies how long the Annotation can be retained, @Rentention contains a member variable of type Retentionpolicy, and must be used when @Rentention You must specify a value for the value member variable:
Retentionpolicy.source: Compiler directly discards comments for this policy
Retentionpolicy.class: The compiler will record the comments in the CLASS file. The JVM does not preserve annotations when running Java programs. This is the default value
Retentionpolicy.runtime: The compiler will record the comments in the class file. When you run a Java program, the JVM preserves the comments. The program can get the comment by reflection
@Target: Used to decorate the Annotation definition to specify which program elements the decorated Annotation can use to decorate. The @Target also contains a member variable named value.
@Documented: The Annotation class that is used to specify that the Annotation is decorated by the meta-javadoc tool is extracted as a document.
Annotations defined as documented must have the retention value set to runtime.
@Inherited: The Annotation that it modifies will have inheritance. If a class uses a Annotation that is @Inherited decorated, its subclasses automatically have that annotation
In practical applications, use less
Java Enumerations & Annotations