關於java 註解中元註解Inherited的使用詳解

轉自:http://blog.csdn.net/snow_crazy/article/details/39381695

關於java中元註解Inherited 的使用說明

首先解釋下元註解,就是用來中聲明註解類型時需要使用到的註解。

Inherited作用是,使用此註解聲明出來的自定義註解,在使用此自定義註解時,如果註解在類上面時,子類會自動繼承此註解,否則的話,子類不會繼承此註解。這裏一定要記住,使用Inherited聲明出來的註解,只有在類上使用時纔會有效,對方法,屬性等其他無效。

下面看下代碼就明瞭了。

[java] view plain copy
  1. /** 
  2.  * 聲明的此註解使用了Inherited元註解,表示此註解用在類上時,會被子類所繼承 
  3.  * @author crazy 
  4.  */  
  5. @Retention(RetentionPolicy.RUNTIME)  
  6. @Inherited  
  7. public @interface InheritedTest {  
  8.   
  9.     String value();  
  10. }  

[java] view plain copy
  1. /** 
  2.  * 聲明的此註解沒有使用Inherited元註解,表示此註解用在類上時,不會被子類所繼承 
  3.  * @author crazy 
  4.  */  
  5. @Retention(RetentionPolicy.RUNTIME)  
  6. public @interface InheritedTest2 {  
  7.   
  8.     String value();  
  9. }  
[java] view plain copy
  1. /** 
  2.  * 父類 
  3.  * @author crazy 
  4.  */  
  5. @InheritedTest("使用Inherited的註解 class")  
  6. @InheritedTest2("未使用Inherited的註解 class")  
  7. public class Parent {  
  8.   
  9.     @InheritedTest("使用Inherited的註解 method")  
  10.     @InheritedTest2("未使用Inherited的註解 method")  
  11.     public void method(){  
  12.           
  13.     }  
  14.     @InheritedTest("使用Inherited的註解 method2")  
  15.     @InheritedTest2("未使用Inherited的註解 method2")  
  16.     public void method2(){  
  17.           
  18.     }  
  19.       
  20.     @InheritedTest("使用Inherited的註解 field")  
  21.     @InheritedTest2("未使用Inherited的註解 field")  
  22.     public String a;  
  23. }  

[java] view plain copy
  1. /** 
  2.  * 子類  只繼承了一個method方法 
  3.  * @author crazy 
  4.  */  
  5. public class Child extends Parent {  
  6.   
  7.     @Override  
  8.     public void method() {  
  9.     }  
  10. }  

[java] view plain copy
  1. /** 
  2.  * 通過反射進行測試 
  3.  * @author crazy 
  4.  */  
  5. public class test {  
  6.   
  7.     public static void main(String[] args) throws NoSuchMethodException, SecurityException, NoSuchFieldException {  
  8.         Class<Child> clazz = Child.class;  
  9.         //對類進行測試   
  10.         System.out.println("對類進行測試");  
  11.         if(clazz.isAnnotationPresent(InheritedTest.class)){  
  12.             System.out.println(clazz.getAnnotation(InheritedTest.class).value());  
  13.         }  
  14.         if(clazz.isAnnotationPresent(InheritedTest2.class)){  
  15.             System.out.println(clazz.getAnnotation(InheritedTest2.class).value());  
  16.         }  
  17.         System.out.println();  
  18.         //對方法 進行測試  
  19.         System.out.println("對方法進行測試");  
  20.         Method method = clazz.getMethod("method"null);  
  21.         if(method.isAnnotationPresent(InheritedTest.class)){  
  22.             System.out.println(method.getAnnotation(InheritedTest.class).value());  
  23.         }  
  24.         if(method.isAnnotationPresent(InheritedTest2.class)){  
  25.             System.out.println(method.getAnnotation(InheritedTest2.class).value());  
  26.         }  
  27.         System.out.println();  
  28.         //對方法2 進行測試  
  29.         System.out.println("對方法2進行測試");  
  30.         Method method2 = clazz.getMethod("method2"null);  
  31.         if(method2.isAnnotationPresent(InheritedTest.class)){  
  32.             System.out.println(method2.getAnnotation(InheritedTest.class).value());  
  33.         }  
  34.         if(method2.isAnnotationPresent(InheritedTest2.class)){  
  35.             System.out.println(method2.getAnnotation(InheritedTest2.class).value());  
  36.         }  
  37.         System.out.println();  
  38.         //對屬性測試  
  39.         System.out.println("對屬性進行測試");  
  40.         Field field = clazz.getField("a");  
  41.         if(field.isAnnotationPresent(InheritedTest.class)){  
  42.             System.out.println(field.getAnnotation(InheritedTest.class).value());  
  43.         }  
  44.         if(field.isAnnotationPresent(InheritedTest2.class)){  
  45.             System.out.println(field.getAnnotation(InheritedTest2.class).value());  
  46.         }  
  47.     }  
  48. }  

下面是輸出結果

[java] view plain copy
  1. 對類進行測試  
  2. 使用Inherited的註解 class  
  3.   
  4. 對方法進行測試  
  5.   
  6. 對方法2進行測試  
  7. 使用Inherited的註解 method2  
  8. 未使用Inherited的註解 method2  
  9.   
  10. 對屬性進行測試  
  11. 使用Inherited的註解 field  
  12. 未使用Inherited的註解 field  

由上可以看出,通過Inherited元註解聲明的自定義註解,在類上使用時,可以被子類繼承,對第一個方法進行測試時,由於子類繼承了父類方法,且兩個都沒有輸出,證明Inherited對方法無效,由方法2可以看出,因爲子類沒有重寫父類方法,所以是直接使用的父類方法,所以兩個都會輸出,同理屬性也是,都會輸出。

所以證明最後結論:通過對註解上使用元註解Inherited聲明出的註解,在使用時用在類上,可以被子類所繼承,對屬性或方法無效。


發佈了141 篇原創文章 · 獲贊 30 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章