註解的簡單例子
/**
* @author 鄧聰
*應用在類上的註解
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Class_anno {
String name() default "congge";
}
//===================================================
/**
* @author 鄧聰
*應用在方法上的註解
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Method_anno {
int i() default 100;
}
//==================================================
/**
* @author 鄧聰
* 註解測試(註解處理器)
*
*/
public class Test{
public static void main(String[] args) throws Exception, Exception {
Annota annota = new Annota();
System.out.println(annota.getClass().getAnnotation(Class_anno.class).name());
System.out.println(annota.getClass().getMethod("haha").getAnnotation(Method_anno.class).i());
}
}
//====執行結果=============
congge
100
註解的簡單例子