Java注释ElementType常量是什么意思? [英] What do Java annotation ElementType constants mean?
问题描述
java.lang.annotation.ElementType
:
程序元素类型。此枚举类型的常量提供Java程序中声明的元素的简单分类。这些常量与 <$ c一起使用$ c>目标 元注释类型,用于指定使用注释类型的合法位置。
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 - 本地变量声明
- 方法 - 方法声明
- 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;
For more info on PACKAGE annotations see here and here.
这篇关于Java注释ElementType常量是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!