javaAPI元注解之Inherited
1、先看注解的声明: (1)Documented:生成API帮助文档时显示注解。 (2)Retention:jvm保留。 (3)Target:此注解是元注解。


3、创建测试类和四个有继承关系的类。getAnnotations查找类的注解,并输出获取到的注解个数。

5、查看输出,Son1类只有一个注解MyInheritedAnnotation2。为什么哪?看这个继承的关系,Son1继承了Fath髫潋啜缅er1,Father1继承了GrandFather,GrandFather继承了Monkey,Monkey继承了Object。Object类没有注解;Monkey有一个注解Deprecated,但是Deprecated注解没有声明Inherited,所以没有注解遗传给GrandFather。GrandFather有一个注解MyInheritedAnnotation3,但是MyInheritedAnnotation3没有声明Inherited,所以不会遗传,GrandFather也没有从Monkey继承到注解,所以GrandFather没有注解遗传给Father1;Father1有一个注解MyInheritedAnnotation2,MyInheritedAnnotation2声明了Inherited,所以会遗传给Son1;Father1没有从GrandFather继承到注解。Son1本身没有注解,但是从其父类Father1继承到了MyInheritedAnnotation2,所以其有一个注解MyInheritedAnnotation2。注意一点:Son1.class.getAnnotations();获取的是类的注解,方法等其他元素的注解不会包括在内,所以MyInheritedAnnotation1没有输出。
