还是显示子类注解
为什么我的继承了
还是显示子类的注解(包括方法)
parent
@Description(author = "denga", desc = "i am interface") public class Parent { public String name(){ return null; }; @Description(author = "denga", desc = "i am interface method") public int age(){ return 0; }; @Deprecated public void sing(){ }; }
@Description(author = "deng", desc = "I am class annotation") public class Child extends Parent { @Description(desc="i am name method annotation", author = "Deng") @Override //标识注解 public String name() { // TODO 自动生成的方法存根 return null; } @Override public int age() { // TODO 自动生成的方法存根 return 0; } @Override public void sing() { // TODO 自动生成的方法存根 System.out.println("i singing love"); } }
public class PareAnn { //1.使用加载器加载类 public static void main(String[] args) { Class c; try { c = Class.forName("com.smile.zhujie.Child"); //2.找到类上面的注解 boolean isExit=c.isAnnotationPresent(Description.class); if(isExit){ //3.拿到注解实例 Description d=(Description) c.getAnnotation(Description.class); System.out.println(d.author()); } //4.找到方法上的注解 Method[] ms =c.getMethods(); for (Method method : ms) { boolean isMExit=method.isAnnotationPresent(Description.class); if(isMExit){ //3.拿到注解实例 Description d=(Description) c.getAnnotation(Description.class); System.out.println(d.desc()); } } //另外的解析方法 for (Method method : ms) { Annotation [] as=method.getAnnotations(); for(Annotation a:as ){ if(a instanceof Description){ Description d=(Description)a; System.out.println(d.desc()); } } } } catch (ClassNotFoundException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } }