자바 반사 메커니즘 - 주석 획득

4631 단어 Java
더 읽 기importjava.lang.annotation.Retention;
importjava.lang.annotation.RetentionPolicy;
importjava.lang.reflect.Method;

@Retention(RetentionPolicy.RUNTIME)
@interfaceMySingle{
intvalue();//thisvariablenamemustbevalue
}
: , java , , , , @Retention(RetentionPolicy.RUNTIME)
, , Annotation ,

classSingle{
@MySingle(100)
publicstaticvoidmyMeth(){
Singleob=newSingle();

try{
Methodm=ob.getClass().getMethod("myMeth");

MySingleanno=m.getAnnotation(MySingle.class);

System.out.println(anno.value());//displays100

}catch(NoSuchMethodExceptionexc){
System.out.println("MethodNotFound.");
}
}

publicstaticvoidmain(Stringargs[]){
myMeth();
}
}

좋은 웹페이지 즐겨찾기