Home>Article>Java> Annotations for java basics

Annotations for java basics

王林
王林 forward
2019-11-27 16:51:11 2373browse

Annotations for java basics

1. Meta-annotation

1.1@Target

[Function]

is used to specify the location where the marked annotation can be used, for example:@Target(ElementType.METHOD): Indicates that it can be used on methods, but other structures cannot be used;@Target({ElementType .METHOD, ElementType.TYPE}): Indicates that it can be used on methods, interfaces, classes, and enumerations.

Related online video tutorials:java course

1.2@Retention

[Function]

Use In the designated annotation retention stage, the annotation has three values:

@Retention(RetentionPolicy.SOURCE): indicates that it is retained until the source code stage and disappears after compilation

@Retention(RetentionPolicy.CLASS): Retains until the compilation phase and disappears after running

@Retention(RetentionPolicy.RUNTIME): Retains until the running phase , if you want to read the annotation information through reflection, you need to specify the annotation retention stage as RUNTIME

1.3@Inherited

[Function]

indicates this Whether the annotation can be inherited by subclasses.

1.4@Documented

[Function]

Indicates whether this annotation can be read into the document by Javadoc.

2. Annotation statement

[Format]

[Meta-annotation]

【修饰符】 @interface 注解名 { 注解体 }

[Example]

@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface MyAnnotation { }

3. Configuration parameter declaration

[Format]

【数据类型】 参数名() default 默认值;

default Default value: When you need to set the default value, you can add it. When you need to set it, you don’t need to write it;

Data types can only be: basic data types, String, Class, enum, Annotation, and one-dimensional arrays of all the above types.

If there is only one parameter member or a frequently used parameter, you can define the parameter name: value. When using annotations, if the parameter name is value, you can omit it and write the input value directly.

[Example]

@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface MyAnnotation { String name() default ""; }

4. Read annotation information

Only annotation annotations@Retention(RetentionPolicy.RUNTIME)It can be read through reflection.

Read annotation information through reflection, as follows:

import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; public class TestInterface { public static void main(String[] args) { MyAnnotation myAnnotation = MyClass.class.getAnnotation(MyAnnotation.class); String value = myAnnotation.value(); System.out.println(value); } } @MyAnnotation class MyClass {} @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @interface MyAnnotation { String value() default "我是一个注解"; }

Output results:

Annotations for java basics

Recommended related article tutorials :java introductory learning

The above is the detailed content of Annotations for java basics. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete