Java-12 enumeration classes and annotations

Posted by frosty1433 on Fri, 04 Mar 2022 14:40:52 +0100

Java-12 enumeration classes and annotations

1, Enum class (enum)

1. Use of enumeration class

  • 1. Understanding of enumerating classes: there are only a limited number of objects in a class, which is uncertain. We call this class enumeration class

  • 2. When you need to define a set of constants, it is strongly recommended to use enumeration classes

  • 3. If there is only one object in the enumeration class, it can be used as the implementation of singleton mode.
    *

    2. How to define enumeration classes

  • Method 1: jdk5 Custom enumeration class before 0

  • Mode 2: jdk5 0, enum keyword can be used to define enumeration class
    *

    3. Common methods in Enum class:

  • values() method: returns an array of objects of enumeration type. This method can easily traverse all enumerated values.

  • valueOf(String str): you can convert a string into a corresponding enumeration class object. The string is required to be the "name" of the enumeration class object. If not, there will be a runtime exception: IllegalArgumentException.

  • toString(): returns the name of the object constant of the current enumeration class

  • 4. Use enum keyword to define the enumeration class to implement the interface

  • Case 1: implement the interface and implement the abstract method in the enum class

  • Case 2: let the objects of the enumeration class implement the abstract methods in the interface respectively

public class SeasonTest {

    public static void main(String[] args) {
        Season spring = Season.SPRING;
        System.out.println(spring);

    }

}

//Custom enumeration class
class Season{
    //1. Declare the attribute of Season object: private final modifier
    private final String seasonName;
    private final String seasonDesc;

    //2. Privatize the constructor of the class and assign a value to the object attribute
    private Season(String seasonName,String seasonDesc){
        this.seasonName = seasonName;
        this.seasonDesc = seasonDesc;
    }

    //3. Provide multiple objects of the current enumeration class: public static final
    public static final Season SPRING = new Season("spring","in the warm spring , flowers are coming out with a rush");
    public static final Season SUMMER = new Season("summer","Summer heat");
    public static final Season AUTUMN = new Season("autumn","fresh autumn weather");
    public static final Season WINTER = new Season("winter","a world of ice and snow");

    //4. Other requirements 1: get the properties of enumeration objects
    public String getSeasonName() {
        return seasonName;
    }

    public String getSeasonDesc() {
        return seasonDesc;
    }
    //4. Other claims 1: toString()
    @Override
    public String toString() {
        return "Season{" +
                "seasonName='" + seasonName + '\'' +
                ", seasonDesc='" + seasonDesc + '\'' +
                '}';
    }
}

2, Annotation

2.1 use of notes

    1. Understanding Annotation:
  • ① New features in jdk 5.0

  • ② Annotation is actually a special tag in the code. These tags can be read and processed during compilation, class loading and runtime. By using annotation,

  • Programmers can embed some supplementary information in the source file without changing the original logic.

  • ③ In Java se, annotations are used for simple purposes, such as marking outdated functions, ignoring warnings, etc. In Java EE / Android

  • Annotations play a more important role, such as configuring any aspect of the application, instead of the cumbersome legacy of the older versions of Java EE

  • Code and XML configuration.

    1. Use example of annotation
  • Example 1: generate annotations related to documents

  • Example 2: format checking during compilation (three basic annotations built into JDK)
    @Override: restrict overriding parent class methods. This annotation can only be used for methods
    @Deprecated: used to indicate that the modified element (class, method, etc.) is obsolete. Usually because the modified structure is dangerous or there is a better choice
    @SuppressWarnings: suppress compiler warnings

  • Example 3: track code dependencies and realize the function of replacing configuration files
    *

    1. How to customize annotations: refer to the definition of @ SuppressWarnings
    • ① The annotation is declared as: @ interface
    • ② Internally defined members, usually represented by value
    • ③ You can specify the default value of the member, which is defined by default
    • ④ If the user-defined annotation has no members, it indicates that it is an identification function.

    If the annotation has members, you need to specify the value of the member when using the annotation.
    Custom annotations must be accompanied by the information processing flow of annotations (using reflection) to make sense.
    The user-defined annotation will indicate two meta annotations: Retention and Target

    1. Four meta annotations provided by jdk

    Meta annotation: an annotation that explains existing annotations
    Retention: Specifies the lifecycle of the decorated Annotation: SOURCE\CLASS (default behavior) \ RUNTIME
    Only annotations declared as RUNTIME lifecycles can be obtained through reflection.
    Target: used to specify which program elements can be modified by the modified Annotation
    The frequency of occurrence is low
    Documented: indicates that the modified annotation will be retained when it is parsed by javadoc.
    Inherited: the Annotation modified by it will be inherited.

5. Obtain annotation information through reflection - the system will explain the reflected content

  1. New features of annotation in jdk 8: repeatable annotation and type annotation
 6.1 Repeatable notes:① stay MyAnnotation Statement on@Repeatable,Member value is MyAnnotations.class
              ② MyAnnotation of Target and Retention Isometric annotation and MyAnnotations Same.

 6.2 Type notes:
 ElementType.TYPE_PARAMETER Indicates that the annotation can be written in the declaration statement of type variables (e.g. generic declaration).

ElementType.TYPE_USE indicates that the annotation can be written in any statement using the type.

/**
 * @author shkstart
 * @create 2019 11:56 am
 */
@Inherited
@Repeatable(MyAnnotations.class)
@Retention(RetentionPolicy.RUNTIME)
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE,TYPE_PARAMETER,TYPE_USE})
public @interface MyAnnotation {

    String value() default "hello";
}
/*
* @Author: Jerry
* @Description: MyAnnotation
* @param:
 * @param null
* @return:
* @Date: 21:00 2021/1/24
*/
//  * 3.  How to customize annotations: refer to the definition of @ SuppressWarnings
//  *① The annotation is declared as: @ interface
//  *② Internally defined members, usually represented by value
//  *③ You can specify the default value of the member, which is defined by default
//  *④ If the user-defined annotation has no members, it indicates that it is an identification function.
@Target({TYPE,FIELD,METHOD,PARAMETER,CONSTRUCTOR,LOCAL_VARIABLE})//Deleting the corresponding location will have an impact
public @interface Note 02 {
//    String value();
    String value() default "hello";
}
effect.
@Target({TYPE,FIELD,METHOD,PARAMETER,CONSTRUCTOR,LOCAL_VARIABLE})//Deleting the corresponding location will have an impact
public @interface Note 02 {
//    String value();
    String value() default "hello";
}

Topics: Java Annotation