@Retention(RetentionPolicy.RUNTIME)
public @interface PersonInfo {
String name();
int age() default 20;
String gender();
}
public class PersonOpe {
@PersonInfo(name="李四",age=20,gender="男")
public void show(String name,int age,String gen) {
System.out.println(name);
System.out.println(age);
System.out.println(gen);
}
}
public class Demo2 {
public static void main(String[] args) throws Exception{
PersonOpe ope=new PersonOpe();
Class<?> class1=PersonOpe.class;
Method method = class1.getMethod("show", String.class,int.class,String.class);
PersonInfo annotation = method.getAnnotation(PersonInfo.class);
String name=annotation.name();
int age=annotation.age();
String gender=annotation.gender();
method.invoke(ope, name,age,gender);
}
}
使用反射获取注解信息
最新推荐文章于 2025-04-03 09:50:14 发布