Java Annotation

来源:互联网 发布:windows平板模式模拟器 编辑:程序博客网 时间:2024/05/18 03:02
package annotation;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;import java.lang.reflect.Method;//取值(ElementType)有://1.CONSTRUCTOR:用于描述构造器//2.FIELD:用于描述域//3.LOCAL_VARIABLE:用于描述局部变量//4.METHOD:用于描述方法//5.PACKAGE:用于描述包//6.PARAMETER:用于描述参数//7.TYPE:用于描述类、接口(包括注解类型) 或enum声明  //取值(RetentionPoicy)有://1.SOURCE:在源文件中有效(即源文件保留)//2.CLASS:在class文件中有效(即class保留)//3.RUNTIME:在运行时有效(即运行时保留)@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) //@Inherited@interface Test{    public int id() default 1;    public String value() default "hello world";}class People{    @Test(id=2)    public void fun(){}}public class Main {    public static void main(String[] args) {        Method methods[]=People.class.getDeclaredMethods();        for(int i=0;i<methods.length;i++){            Test test=methods[i].getAnnotation(Test.class);            if(test!=null){                System.out.println(test.id()+" "+test.value());            }        }    }}
0 0
原创粉丝点击