Java注解ElementType常量是什么意思?

What do Java annotation ElementType constants mean?(Java注解ElementType常量是什么意思?)
本文介绍了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常量是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)