java元注解@Inherited的使用詳解
1.先看源碼文檔
/** * 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. * * @author Joshua Bloch * @since 1.5 * @jls 9.6.3.3 @Inherited */ @Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) public @interface Inherited { }
上述代碼注釋部分可以用谷歌翻譯大法大致意思是
表示注釋類型自動(dòng)繼承。 如果在注釋類型聲明中存在繼承的元注釋,并且用戶在類聲明上查詢注釋類型,并且類聲明沒(méi)有此類型的注釋,則該類的超類將自動(dòng)查詢注釋類型。 將重復(fù)此過(guò)程,直到找到此類型的注釋,或者達(dá)到類層次結(jié)構(gòu)(Object)的頂部。 如果沒(méi)有超類具有此類型的注釋,則查詢將指示所討論的類沒(méi)有這樣的注釋。
請(qǐng)注意,如果使用注釋類型來(lái)注釋除類之外的任何內(nèi)容,則此元注釋類型不起作用。 還要注意,這個(gè)元注釋只會(huì)導(dǎo)致從超類繼承注釋; 已實(shí)現(xiàn)的接口上的注釋無(wú)效。
通過(guò)上述描述可知,使用該注解的注解父類的子類可以繼承父類的注解。
2.代碼測(cè)試
2.1擁有@Inherited注解
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Inherited public @interface InheritedTest { String value(); }
@InheritedTest("擁有Inherited") public class Person { public void method(){ } public void method2(){ } }
public class Student extends Person { }
測(cè)試:
public class TestInherited { public static void main(String[] args) { Class<Student> studentClass = Student.class; if (studentClass.isAnnotationPresent(InheritedTest.class)){ System.out.println(studentClass.getAnnotation(InheritedTest.class).value()); } } }
輸出:
2.2未擁有@Inherited注解
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface IsNotInherited { String value(); }
@IsNotInherited("未擁有Inherited") public class Person { public void method(){ } public void method2(){ } }
public class Student extends Person { }
測(cè)試:
public class TestInherited { public static void main(String[] args) { Class<Student> studentClass = Student.class; if (studentClass.isAnnotationPresent(IsNotInherited.class)){ System.out.println(studentClass.getAnnotation(IsNotInherited.class).value()); } } }
沒(méi)有輸出任何任容,由此可知未擁有@Inherited注解的注解的類的子類不會(huì)被繼承該注解。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java?常量池詳解之class文件常量池?和class運(yùn)行時(shí)常量池
這篇文章主要介紹了Java?常量池詳解之class文件常量池?和class運(yùn)行時(shí)常量池,常量池主要存放兩大類常量:字面量,符號(hào)引用,本文結(jié)合示例代碼對(duì)java class常量池相關(guān)知識(shí)介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12Java 如何從spring容器中獲取注入的bean對(duì)象
這篇文章主要介紹了Java 如何從spring容器中獲取注入的bean對(duì)象,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下2020-11-11使用Sharding-JDBC對(duì)數(shù)據(jù)進(jìn)行分片處理詳解
這篇文章主要介紹了使用Sharding-JDBC對(duì)數(shù)據(jù)進(jìn)行分片處理詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10Java使用POI導(dǎo)出Excel(二):多個(gè)sheet
這篇文章介紹了Java使用POI導(dǎo)出Excel的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-10-10Java十大經(jīng)典排序算法的實(shí)現(xiàn)圖解
Java常見的排序算法有:插入排序、希爾排序、選擇排序、冒泡排序、歸并排序、快速排序、堆排序、基數(shù)排序等。本文詳解介紹Java十大十大經(jīng)典排序算法的實(shí)現(xiàn)以及圖解,需要的可以參考一下2022-03-03SpringBoot中HttpSessionListener的簡(jiǎn)單使用方式
這篇文章主要介紹了SpringBoot中HttpSessionListener的簡(jiǎn)單使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03SpringBoot2開發(fā)從0開始Spring?Initailizr初始化
這篇文章主要為大家介紹了SpringBoot2從0開始lombok、devtools、Spring?Initailizr的開發(fā)技巧,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05親手教你SpringBoot中的多數(shù)據(jù)源集成問(wèn)題
本文主要是介紹基于springboot的多數(shù)據(jù)源切換,輕量級(jí)的一種集成方案,對(duì)于小型的應(yīng)用可以采用這種方案,我之前在項(xiàng)目中用到是因?yàn)楹?jiǎn)單,便于擴(kuò)展以及優(yōu)化,對(duì)SpringBoot多數(shù)據(jù)源集成問(wèn)題感興趣的朋友一起看看吧2022-03-03JavaWeb Refresh響應(yīng)頭代碼實(shí)例詳解
這篇文章主要介紹了JavaWeb Refresh響應(yīng)頭代碼實(shí)例詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02