What do Java annotation ElementType constants mean?(Java注解ElementType常量是什么意思?)
问题描述
java.lang.annotation.ElementType:
一种程序元素类型.这种枚举类型的常量提供了 Java 程序中声明元素的简单分类.这些常量与 一起使用目标元注解类型来指定使用注解类型合法的地方.
A program element type. The constants of this enumerated type provide a simple classification of the declared elements in a Java program. These constants are used with the Target meta-annotation type to specify where it is legal to use an annotation type.
有以下常量:
- ANNOTATION_TYPE - 注释类型声明
- CONSTRUCTOR - 构造函数声明
- FIELD - 字段声明(包括枚举常量)
- LOCAL_VARIABLE - 局部变量声明
- METHOD - 方法声明
- PACKAGE - 包声明
- PARAMETER - 参数声明
- TYPE - 类、接口(包括注解类型)或枚举声明
- ANNOTATION_TYPE - Annotation type declaration
- CONSTRUCTOR - Constructor declaration
- FIELD - Field declaration (includes enum constants)
- LOCAL_VARIABLE - Local variable declaration
- METHOD - Method declaration
- PACKAGE - Package declaration
- PARAMETER - Parameter declaration
- TYPE - Class, interface (including annotation type), or enum declaration
有人能解释一下它们各自是什么吗(在实际代码中它们会被注释的地方)吗?
Can someone explain what each of them are (where they'd be annotated in actual code)?
推荐答案
总结一下主要的:
@CustomTypeAnnotation
public class MyAnnotatedClass {
@CustomFieldAnnotation
private String foo;
@CustomConstructorAnnotation
public MyAnnotatedClass() {
}
@CustomMethodAnnotation
public String bar(@CustomParameterAnnotation String str) {
@CustomLocalVariableAnnotation String asdf = "asdf";
return asdf + str;
}
}
ANNOTATION_TYPE 是另一个注解上的注解,像这样:
ANNOTATION_TYPE is an annotation on another annotation, like this:
@CustomAnnotationTypeAnnotation
public @interface SomeAnnotation {
..
}
包在包中的一个package-info.java文件中定义,像这样:
Package is defined in a package-info.java file in the package, like this:
@CustomPackageLevelAnnotation
package com.some.package;
import com.some.package.annotation.PackageLevelAnnotation;
有关 PACKAGE 注释的更多信息,请参阅此处和这里.
For more info on PACKAGE annotations see here and here.
这篇关于Java注解ElementType常量是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java注解ElementType常量是什么意思?
基础教程推荐
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 修改 void 函数的输入参数,然后读取 2022-01-01
