赞
踩
①deprecation:使用已被@Deprecated标注的程序元素;
②unused:程序含有未被使用的元素;
③serial:在可序列化的类上缺少serialVersionUID定义;
- [public] @interface 注解名 {
- [属性1;]
- [属性2;]
- ...
- [属性n;]
- }
数据类型 属性名() [default 默认属性值];
Java提供了四个用于修饰自定义注解的元注解:@Target、@Retention 、@Documented和@Inherited。
eclipse中的操作如下:
- package demo;
-
- import java.lang.annotation.Documented;
- import java.lang.annotation.ElementType;
- import java.lang.annotation.Inherited;
- import java.lang.annotation.Retention;
- import java.lang.annotation.RetentionPolicy;
- import java.lang.annotation.Target;
-
- @Target(value=ElementType.METHOD) //此注解只能用于方法
- @Inherited //子类可以继承该注解
- @Retention(RetentionPolicy.RUNTIME) //此注解可以在运行java程序时通过反射获取到。
- public @interface Override {
- String[] value(); //未设置默认值,则在java文件中使用此注解时为value赋值,并且数组只有一个值时,“{}”可以省略。
- }
- package demo;
-
- import java.lang.annotation.Annotation;
- import java.lang.reflect.Method;
-
- public class Son extends Father{
- public static void main(String[] args) throws NoSuchMethodException, SecurityException {
- Class class1=Son.class;
- Method method=class1.getMethod("study");
- Annotation[] annotation=method.getAnnotations();
- for (Annotation annotation2 : annotation) {
- System.out.println(annotation2);
- }
- }
- }
- class Father {
- @Override(value = { "111" })
- public static void study() {
-
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。