Java notes--Enumerations & annotations

Source: Internet
Author: User

1, the implementation of the custom enumeration class, for example:

classseason{//1, provides the properties of the class, declared as Rivate final    Private FinalString name; Private FinalString Desc; //2, property declared as final, initialized in constructor, constructor private    PrivateSeason (string name, String desc) { This. Name =name;  This. Desc =desc; }    //3, calling properties by public method     PublicString GetName () {returnname; }     PublicString GetDesc () {returnDesc; }    //4, create object, decorate with public static final     Public Static FinalSeason SPRING =NewSeason ("Spring", "Springtime");  Public Static FinalSeason SUMMER =NewSeason ("Summer", "Summer");  Public Static FinalSeason AUTUMN =NewSeason ("Autumn", "Autumn");  Public Static FinalSeason WINTER =NewSeason ("Winter", "Winter"); @Override PublicString toString () {return"Season [name=" + name + ", desc=" + Desc + "]"; }    }

2. Use the enum keyword to define the enumeration class:

 Public enumSeason {SPRING ("Spring", "Springtime"), SUMMER ("Summer", "Summer"), AUTUMN ("Autumn", "Autumn"), WINTER ("Winter", "Winter"); Private FinalString name; Private FinalString desc;  PublicString GetName () {returnname; }     PublicString GetDesc () {returndesc; }        PrivateSeason (string name, String desc) { This. Name =name;  This. desc =desc; }}

3. Common methods for enumerating classes:

1) values ();

2) vauueof (String name);

// 1.values () Season[] Seasons = season.values ();  for (int i = 0; i < seasons.length; i++) {    System.out.println (seasons[i]);} // 2.valueOf (String name): The parameter passed in must be the name of the object that the enumeration class exists in // Otherwise, the java.lang.IllegalArgumentException exception is reported. String str = "SPRING"= season.valueof (str); SYSTEM.OUT.PRINTLN (season);

4. Enumeration classes can also implement interfaces, allowing different enum class objects to invoke overridden abstract methods, with different execution effects (each object is rewritten separately).

5, Element according to (MetaData), is the annotation (Annotation), is a special tag in the code, these tags can be compiled, class loading, runtime is read, and perform the corresponding processing, by using Annotation, the programmer can not change the original logic of the case, Embed some supplemental information in the source file.

6, annotation can be used like modifiers, can be used to decorate the package, class, constructor, method, member variables, parameters, local variables of the declaration, the information is saved in the annotation "Name=value" pair.

7. Annotation can be used to set meta data for program elements.

8.3 Basic annotation types in JDK:

1) @Override: Restrict overriding of the parent class method. This comment can only be used for methods;

2) @Deprecated: Used to indicate that a program element (class, method, etc.) is obsolete;

3) @SuppressWarnings: Suppresses compiler warnings.

9. Custom annotation:

 public @Interface  myannotation {    default "Myannotation";}

10. The meta-annotation of the JDK is used to modify other annotation definitions, and JDK5.0 provides the annotation types specifically on the annotations, respectively:

1) 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, you must specify a value for that value member variable when using @Rentention (RETENTIONPOLICY.SOURC E, Retentionpolicy.class, Retentionpolicy.runtime)

2) Target: Used to modify the annotation definition to specify which program elements the modified annotation can use to decorate, @Target also contains a member variable named value.

3) Documented: Used to specify that the annotation class that is decorated by the meta annotation will be extracted by the Javadoc tool into a document (the annotations defined as documented must have the retention value set to runtime).

4) Inherited: The annotation that it modifies will have inheritance, and if a class uses annotation that is @inherited decorated, its subclasses will automatically have that annotation (less used).

Java notes--Enumerations & annotations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.