Defined Note (a), the custom annotation and use

annotation:

Definitions : annotation ( Annotation ), also known as metadata. Describe a code level. It is a feature of JDK1.5 and later introduced the classes, interfaces, enumerations are on 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 will be described, the comment.

Note: the programmer to read

Note: to see the JVM

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]

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.

Custom Note: Use the keyword @interface

Note: also .java file defines the use of annotations. Also compiled .class file

Definition Format

元注解
public @interface 注解名称{
	属性列表;
}

E.g:

package com.ccc.demo05Annotation;

public @interface MyAnnotation01 {
}
  • Notes on essentially a interface that inherits default Annotation Interface
public interface MyAnno extends java.lang.annotation.Annotation {}

Attribute contains a definition of annotation

  1. The role of property

    • It allows users to use annotations to pass parameters , make annotations and more powerful. Annotation attributes we can be seen as an abstract method, but have default values
  2. Formatting attributes

public @interface MyAnnotation02 {
	修饰符 数据类型 属性名();
	修饰符 数据类型 属性名() default 默认值;
}

Modifiers: Fixed using public abstract, can be omitted; but it is recommended to write, to enhance readability statement

  1. Attribute definition Example 1
public @interface Student {
     String name(); // 姓名
     public abstract int age() default 18; // 年龄
     public abstract String gender() default "男"; // 性别
} 
   // 该注解就有了三个属性:name,age,gender

Example 2

public @interface MyAnnotation02 {
    //定义一个int类型的属性
    public abstract int a();
    //定义一个double类型的属性,添加默认值5.5
    double d() default 5.5;
    //定义一个String类型的数组
    public abstract String[] arr();

    //以下类型作为了解
    
    //定义一个反射类型的属性
    public abstract Class clazz();
    
    //定义一个注解类型的属性(MyAnnotation01为本章中第一个案例)
    public abstract MyAnnotation01 my01();
    
    //定义一个枚举类型的属性
    public abstract Color c() default  Color.RED;
}
  1. Suitable types of attribute data

    • Eight basic data types ( int, a float, Boolean, byte, Double, char, Long, Short )
    • String Type, Class type (i.e., reflection type), enumerated types, annotation types
    • All of the above types of one-dimensional array

Enumeration simple example:

package com.itheima.demo05Annotation;

/*
    枚举中的属性,通过枚举的名字可以直接使用(类似于静态)  Color.RED
    相当于以下代码
        public static final Color c1 = new RED();
        public static final Color c1 = new GREEN();
 */
public enum Color {
    RED,GREEN
}

Custom annotation of use:

The use of annotations:

  1. Before the package can be used, the classes, interfaces, the member variables, members method, constructor, local variables, method parameters,
  2. But notes a location with the same name, can only be used once
  3. The same location, notes a different name can be used

Using the format:

@ Annotation name (attribute name = attribute value, attribute name = attribute value, attribute name = attribute value, attribute name = attribute value, attribute name = attribute value, ...)

  1. Not attribute comments @ attribute name can be used directly
  2. There are attributes of the notes, we must use the keys on the way for all attributes are assigned annotations, notes in order to use
       a. There are properties of the default values, you can not assign
       b. Assign multiple properties, you need to use a comma- separated
       c the type of property if an array . Then assign a required {} wrapped up all the values. Array if only one value may be omitted {}
             example: ARR = { "AAA", "BBB", "CCC"}
                        ARR = "AAA"
       . D notes if only one attribute , and name attribute called value , then the assignment when possible key omitted, directly to the write value value
             example: value = 10 -> 10

Special property value

  1. When the annotation is only a name attribute and value, to value property assignment when using annotations directly to the property value, regardless of value is the value of a single element or an array type.
  2. If there are notes in addition to the value property other properties, and at least one property has no default value, then use annotations to assign attribute, value attribute name can not be omitted.

Precautions

  • If the property has a default value, the notes when you can not assign this property.
  • If the property has no default value, then you must give the property assignment when using annotations.

The following example:

Only one attribute defined annotations name value
package com.itheima.demo05Annotation;

public @interface MyAnnotation03 {
    public abstract String value();
}
Use defined earlier comment
package com.ccc.demo05Annotation;

@MyAnnotation01
@MyAnnotation02(a = 10,arr = {"aaa","bbb","ccc"})
public class UseMyAnnotation {
    @MyAnnotation01
    @MyAnnotation03(value = "hello")
    private String name;

    @MyAnnotation02(a = 100,d = 8.8,arr="aaa")
    private int age;

    @MyAnnotation01
    @MyAnnotation03("你好")
    public UseMyAnnotation(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @MyAnnotation01
    public String getName() {
        return name;
    }

    @MyAnnotation02(a = 10,arr = "aaa")
    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

Guess you like

Origin blog.csdn.net/qq_45083975/article/details/91980403