问题描述
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常量是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!