Home > Java > Javagetting Started > body text

Annotations for java basics

王林
Release: 2019-11-27 16:51:11
forward
2386 people have browsed it

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 注解名 { 注解体 }
Copy after login

[Example]

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

3. Configuration parameter declaration

[Format]

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

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 "";
}
Copy after login

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 "我是一个注解";
}
Copy after login

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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!