@Inherited 的作用

在註解上使用@Inherited 表示該註解會被子類繼承,注意,僅針對類,成員屬性、方法並不受此註釋的影響。

對於類來說,子類要繼承父類的註解需要該註解被 @Inherited 標識。
對於成員屬性和方法來說,非重寫的都會保持和父類一樣的註解,而被實現的抽象方法,被重寫的方法都不會有父類的註解。

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.

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.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章