I love the java custom annotation series --- []

First, the overview notes

1. Definitions: annotation (Annotation), also known as metadata. Describe a code level. It is a feature of JDK1.5 and later introduced the class,
  Interfaces, enumerations are at the same level. It can be declared in front of the packages, classes, fields, methods, local variables, parameters and the like of the method for these elements
  Explained, comment.
2. Role Category:
  Writing documentation: annotated code generated by the document identified [for example, generate documents doc documentation]
  Code analysis: analyzed by annotating the code identification code [e.g., annotations reflected]
  Compile check: by annotating code identified so that the compiler can compile basic checks [for example, Override]
3. Common Annotations
  1. @author: used to identify the name of the author
  2. @version: a version number for identifying an object, scope: files, classes, methods.
  3. @Override: used to modify the method declaration, to tell the compiler which is a method override the parent class, parent class if the method does not exist, the compilation fails.
4. custom annotation
/*
    Custom annotation
        1. Format
            public @interface comment Name {
                Attribute Set
            }
        2. Classification
            (1) empty Note: there is no attribute set
 */
public @interface MyAnno01 {
}
/*
    Custom annotation
        1. Format
            public @interface comment Name {
                Attribute Set
            }
        2. Classification
            (2) attribute set annotations
                Attribute definition format:
                Attribute Name Data Type (); // no default
                Data type attribute name () Default default; // has a default value, the content that follows the default
            Type (3) What are the attributes?
                Eight basic types, String, enumeration (no), Class type, annotation types
                And one or more one-dimensional array of any type int []
 */
public @interface MyAnno02 {
    String name();
    int age() default 18;
    String [] hobbies (); // interested 
    MyAnno01 Anno (); // annotation types 
}
/*
    3. Custom Notes annotations used
        1. Empty annotation can be used directly
        2. A comment can only be used once in one location, you can use a number of different notes on a position
        3. If there is annotation attributes, you must use key-value pairs assigns them to assign attribute name = attribute value multiple properties, separated
          If the property is an array type, and it has only one attribute value may be omitted if the {} {} plurality of property values ​​can not be omitted
        4. If the property has no default value must be assigned if there is a default value can not be assigned
        5. If a comment is only one property and the property called value you can omit the property name with particular attention to the assignment of --------------
 */
public class Demo07AnnotationNotice {
    @ MyAnno01
    // MyAnno01 @ // same position, can only be used once 
    @ MyAnno02 (name = "juvenile siege lion " , hobbies = " Learning the Java " , Anno = @ MyAnno01)
    MyAnno03 @ ( " juvenile " )
    @ MyAnno04
    public void method() {

    }
}
/*
    5. If a comment is only one property and the property called value you can omit the property name with particular attention to the assignment of --------------
 */
public @interface MyAnno03 {
    Value String (); // no default 
}
public @interface MyAnno04 {
    String value () default "juvenile siege lion"; // has a default value 
}

Custom annotation example:

/*
    Define a comment: Book
        - Contains property: String value () Title
        - properties comprising: double price () price, the default value is 100
        - contains properties: String [] authors () multiple authors
 */
public @interface Book {
    String value();
    double price() default 100;
    String[] authors();
}

Demonstration using custom annotations:

/*
    Custom Book notes and use cases

        Define a comment: Book
        - Contains property: String value () Title
        - properties comprising: double price () price, the default value is 100
        - contains properties: String [] authors () multiple authors
 * / 
@Book (value = " I love the java series " ,. Price = 1000 , in the authors = { " juvenile " , " girl " })
 public  class Demo08AnnotationBook {
    @Book (value = " Juvenile Revelation " , the authors = " Viagra " )
     public  void Method () {

    }
}

 

Guess you like

Origin www.cnblogs.com/hujunwei/p/12616890.html