Java annotation any enum

Java Enumeration Classes and Annotations

Define the interface Info, and define the method show() in the interface. Each object in the enumeration class implements an override of the show() method. The result of implementation is that different enumeration object electrophoresis methods get different results.

package com.atguigi.java; /** * Define enumeration classes using enum keywords * Description: Defined enumeration classes inherit by default from java.lang.Enum classes */ public class SeasonTest1 < public static void main(String[] args) < Season1 summer = Season1.SUMMER; summer.show(); // Ningxia Season1 winter = Season1.valueOf("WINTER"); winter.show(); // Around winter >> interface Info < void show(); >//Enumerating classes using enum keywords enum Season1 implements Info < // 1. Provide the object of the current enumeration class, and end with "," "Separate, End Object"; SPRING ("spring", "In the warm spring, flowers are coming out with a rush")< @Override public void show() < System.out.println("Where is spring?"); >>, SUMMER("summer", "Scorching summer") < @Override public void show() < System.out.println("Ningxia"); >>, AUTUMN("Autumn", "Fresh autumn weather") < @Override public void show() < System.out.println("Don't come back in autumn"); >>, WINTER("winter", "A world of ice and snow") < @Override public void show() < System.out.println("Around winter"); >>; // 2. Declare the attributes of Season objects: private final decoration private final String seasonName; private final String seasonDesc; // 3. Privatize the constructor of the class and assign a value to the object property private Season1(String seasonName, String seasonDesc) < this.seasonName = seasonName; this.seasonDesc = seasonDesc; >// 4. Other Requirements: Getting the Properties of Enumerated Class Objects public String getSeasonName() < return seasonName; >public String getSeasonDesc() < return seasonDesc; >// 5. Other Requirements: Providing toString @Override public String toString() < return "Season1'; > >

2. notes

2.1 Understanding of Annotations

  • Since JDK 5.0, Java has added support for MetaData, namely Annotation.
  • Annotation is actually a special tag in the code, which can be read at compilation, class loading, runtime and processed accordingly. By using Annotation, programmers can embed additional information in source files without changing the original logic. Code analysis tools, development tools and deployment tools can be validated or deployed through these supplementary information.
  • Annotation can be used as modifiers to modify packages, classes, constructors, methods, member variables, parameters, and declarations of local variables. This information is stored in Annotation’s name = value pair.
  • In JavaSE, annotations are used for simpler purposes, such as marking out outdated functions, ignoring warnings, and so on. Annotations play a more important role in Java EE/Android, such as configuring any aspect of the application, replacing the tedious code and XML configuration left over from the old version of Java EE.
  • Framework = Annotations + Reflections + Design Patterns
Читайте также:  Python sys stdin input

2.2 Common Annotation

  1. Generate document related annotations;
  2. Format checking at compile time (three basic annotations built into JDK)
    • @ Overrive: Limits the method of overriding the parent class, and this annotation can only be used for methods;
    • @ Deprecated: Used to indicate that the modified elements (classes, methods) are outdated, usually because the modified structure is dangerous or there are better choices;
    • @ SuppressWarning: Suppress compiler warnings.
  3. Tracking code dependencies to implement alternative configuration files
  • Servlet 3.0 provides annotations that make it unnecessary to deploy a servlet in a web.xml file.

2.3 How to customize annotations

  • Define a new Annotation type using the @interface keyword
  • Custom annotation automatically implements the java.lang.annotation.Annotation interface.
  • Annotation member variables are declared in the Annotation definition as parametric-free methods. Its method name and return value define the name and type of the member.
  • You can specify an initial value for Annotation’s member variables when defining them. The initial value of the specified member variables can be using the default keyword.
  • If there is only one parameter member, it is recommended to use the parameter name value.
  • Annotation without member definition is called tag; Annotation with member variables is called metadata Annotation.
  • Note: Custom annotations must be accompanied by annotated information processing flow (using reflection) to make sense.

package java.atguigu.java1; public @interface MyAnnotation

Meta-annotations in 2.4 JDK

Meta-Annotation in JDK is used to modify other Annotation definitions. Four standard meta-annotation types are provided in JDK 5.0.

  • @ Retention: Can only be used to modify an Annotation definition to specify the life cycle of the Annotation, and @Retention contains a member variable of Retention Policy type that must be assigned a value when using this annotation:
    • RetentionPolicy.SOURCE: Effective in the source file (i.e. source file retention), the compiler discards the annotations of this policy directly;
    • RetentionPolicy.CLASS: Valid in the class file (i.e. class retention), and JVM does not retain annotations when running the program. This is the default value
    • RetentionPolicy.RUNTIME: Valid at runtime (i.e., runtime retention), and JVM retains annotations when running Java programs. The program obtains the annotation by reflection.

    public enum RetentionPolicy

    New features of annotations in 2.5 JDK8

    Java 8 provides two improvements to annotation processing: repeatable annotations and available types of annotations. In addition, reflection has been enhanced to get the name of the method parameter in Java 8. This simplifies annotations on method parameters.

    • type annotation
      • After JDK8, there are two more enumerated values for the parameter type ElementType of metaannotation @Target: TYPE_PARAMETER and TYPE_USE.
      • Before Java 8, annotations could only be used where they were declared. Java 8 started, and annotations could be applied anywhere.
      • ElementType.TYPE_PARAMETER indicates that the annotation can be written in declarative statements of type variables (such as generic declarations).
      • ElementType.TYPE_USE indicates that the annotation can be written in any statement of the type of use.

      public class TestTypeDefine < private U u; public void test(T t) < >> @Target() @interface TypeDefine
      @MyAnnotation public class AnnotationTest < @MyAnnotation private String name; public static void main(String[] args) < AnnotationTestt = null; int a = (@MyAnnotation int) 2L; @MyAnnotation int b = 10; > public static void method(T t) < >public static void test(@MyAnnotation String arg) throws @MyAnnotation Exception < >> @Target(ElementType.TYPE_USE) @interface MyAnnotation

      Posted on Sat, 12 Oct 2019 05:57:41 -0400 by rhunter007

      Hot Tags

      • Java — 7906
      • Database — 3176
      • Python — 3103
      • Attribute — 2963
      • Programming — 2938
      • Javascript — 2788
      • Spring — 2575
      • xml — 2270
      • Android — 2243
      • Linux — 2204
      • JSON — 2150
      • less — 2137
      • network — 2115
      • github — 2063
      • MySQL — 1760
      • SQL — 1616
      • PHP — 1559
      • encoding — 1360
      • Mobile — 1172
      • Apache — 1137

      Источник

Оцените статью