重慶分公司,新征程啟航
為企業提供網站建設、域名注冊、服務器等服務
為企業提供網站建設、域名注冊、服務器等服務
這篇文章主要介紹了如何在java項目中使用@Inherited元注解的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇如何在java項目中使用@Inherited元注解文章都會有所收獲,下面我們一起來看看吧。
喀什網站建設公司成都創新互聯公司,喀什網站設計制作,有大型網站制作公司豐富經驗。已為喀什上千家提供企業網站建設服務。企業網站搭建\成都外貿網站建設公司要多少錢,請找那個售后服務好的喀什做網站的公司定做!
1.先看源碼文檔
@已記錄
@Retention(保留策略。RUNTIME)
@Target(ElementType。注釋類型)
public@interfaceInherited{
}以上代碼標注部分可以用Google翻譯。大致意思是
指示批注類型的自動繼承。如果在標注類型聲明中有繼承的元標注,用戶在類聲明上查詢標注類型,而類聲明沒有這個類型的標注,那么類的超類會自動查詢標注類型。這個過程將重復進行,直到找到這種類型的注釋或者到達類層次結構的頂部。如果沒有一個超類有這種類型的注釋,查詢將會指出這個類沒有這樣的注釋。
請注意,如果您使用注釋類型來注釋除類之外的任何東西,這種元注釋類型沒有任何效果。還要注意,這個元注釋只會導致從超類繼承注釋;對已實現接口的無效注釋。
從上面的描述可以看出,使用這個標注的標注父類的子類可以繼承父類的標注。
2.代碼測試
2.1擁有@Inherited注解
@Target(ElementType。類型)
@Retention(保留策略。RUNTIME)
@繼承
public @ interfaceInheritedTest {
string value();
}@InheritedTest('擁有繼承')
publicclassPerson{
publicvoidmethod(){
}
publicvoidmethod2(){
}
} publicclassstudentextends person {
}測試:
publicclassTestInherited{
public static void main(String[]args){
CLaSS studentCLaSS=student . class;
if(student class . is notationpresent(inherited test . class)){
system . out . println(StudentClass . GetAnnotation(InheritedTest . Class))。value());
}
}
}輸出:
2.2沒有@Inherited注釋
@Target(ElementType。類型)
@Retention(保留策略。RUNTIME)
public @ interfacesinotinherited {
string value();
} @ IsNotInherited('未擁有繼承')
publicclassPerson{
publicvoidmethod(){
}
publicvoidmethod2(){
}
} publicclassstudentextends person {
}測試:
publicclassTestInherited{
public static void main(String[]args){
CLaSS studentCLaSS=student . class;
if(StudentClass . is notationPresent(iSnotinerIded . Class)){
system . out . println(StudentClass . GetAnnotation(iSNOTiHerided . class))。value());
}
}
}
不輸出容差,可見沒有@Inherited標注的類的子類不會繼承該標注。
關于“如何在java項目中使用@Inherited元注解”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“如何在java項目中使用@Inherited元注解”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注創新互聯行業資訊頻道。