@Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@interface myAnnotation
{
boolean devbuild() default false;
int counter();
}
2.在函数中使用这个annotion为函数加属性
public class MyClass
{
@myAnnotation(devbuild=production,counter=1)
public void myMethod()
{
}
}
3.在程序中获得该函数的annotation并do something
public static void main(String[] args)
{
MyClass mc = new MyClass();
Annotation[] a = mc.getClass().getMethod("myMethod").getAnnotations();
for (int i=0; i<a.length ; i++)
{
System.out.println("a["+i+"]="+a[i]+" ");
}
}
本文地址:http://com.8s8s.com/it/it11558.htm