本文主要是介绍【Java】@Inherited 注解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Java里面有四种元注解,@Inherited便是其中之一,其作用在java文档中写的很清楚了:
* Indicates that an annotation type is automatically inherited. If* an Inherited meta-annotation is present on an annotation type* declaration, and the user queries the annotation type on a class* declaration, and the class declaration has no annotation for this type,* then the class's superclass will automatically be queried for the* annotation type. This process will be repeated until an annotation for this* type is found, or the top of the class hierarchy (Object)* is reached. If no superclass has an annotation for this type, then* the query will indicate that the class in question has no such annotation.** <p>Note that this meta-annotation type has no effect if the annotated* type is used to annotate anything other than a class. Note also* that this meta-annotation only causes annotations to be inherited* from superclasses; annotations on implemented interfaces have no* effect.
表明一个注解是可以被继承的。如果一个注解A被@inherited标注,那么当调用反射api查询一个类是否被A标注不仅仅会看这个类有没有A注解,同时会看A的父类是否有A注解,直到找到A注解或者到达了顶层父类。
这个特性可以理解为子类会继承父类的被@Inherited修饰过的注解。
另外这个效果仅仅作用在类上,对接口无效。
这篇关于【Java】@Inherited 注解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!