ElementType
是一个在 java.lang.annotation
包中定义的枚举类型,它表示 Java 程序元素(program element)的类型,这些程序元素可以被注解(annotation)所修饰。这个枚举类型与 @Target
元注解一起使用,以指定某个注解可以应用于哪些类型的 Java 程序元素。
下面是对 ElementType
枚举中每个值的详细解释:
@MyAnnotation public class MyClass {}
@MyAnnotation private int myField;
@MyAnnotation public void myMethod() {}
public void myMethod(@MyAnnotation String param) {}
@MyAnnotation public MyClass(int value) {}
void myMethod() { @MyAnnotation int localVar; }
@MyAnnotation public @interface MyOtherAnnotation {}
@MyAnnotation package com.example;
public class MyClass<@MyAnnotation T> {}
public void myMethod(List<@MyAnnotation String> list) {}
这个枚举类型与 @Target
元注解一起使用,以指定注解的适用目标。例如,如果你有一个注解只想应用于方法和字段,你可以这样定义它:
@Target({ElementType.METHOD, ElementType.FIELD})
public @interface MyAnnotation {
// ...
}
这意味着 @MyAnnotation
只能被用于方法和字段声明,不能用于其他类型的 Java 程序元素。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有