java 注解

 

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnno {
     String className();
     String methodName();
}
@MyAnno(className = "com.howhy.demo.Person", methodName ="say" )
public class Demo1 {
    public static void main(String[] args) throws Exception {
        Class<Demo1> cls=Demo1.class;
        MyAnno annotation = cls.getAnnotation(MyAnno.class);
        String s = annotation.className();
        String s1 = annotation.methodName();
        Class<?> aClass = Class.forName(s);
        Object constructor = aClass.newInstance();
        Method method = aClass.getMethod(s1);
        method.invoke(constructor);
    }
}

 

posted @ 2021-08-31 17:47  howhy  阅读(13)  评论(0编辑  收藏  举报