轉自:http://blog.csdn.net/snow_crazy/article/details/39381695
關於java中元註解Inherited 的使用說明
首先解釋下元註解,就是用來中聲明註解類型時需要使用到的註解。
Inherited作用是,使用此註解聲明出來的自定義註解,在使用此自定義註解時,如果註解在類上面時,子類會自動繼承此註解,否則的話,子類不會繼承此註解。這裏一定要記住,使用Inherited聲明出來的註解,只有在類上使用時纔會有效,對方法,屬性等其他無效。
下面看下代碼就明瞭了。
- /**
- * 聲明的此註解使用了Inherited元註解,表示此註解用在類上時,會被子類所繼承
- * @author crazy
- */
- @Retention(RetentionPolicy.RUNTIME)
- @Inherited
- public @interface InheritedTest {
- String value();
- }
- /**
- * 聲明的此註解沒有使用Inherited元註解,表示此註解用在類上時,不會被子類所繼承
- * @author crazy
- */
- @Retention(RetentionPolicy.RUNTIME)
- public @interface InheritedTest2 {
- String value();
- }
- /**
- * 父類
- * @author crazy
- */
- @InheritedTest("使用Inherited的註解 class")
- @InheritedTest2("未使用Inherited的註解 class")
- public class Parent {
- @InheritedTest("使用Inherited的註解 method")
- @InheritedTest2("未使用Inherited的註解 method")
- public void method(){
- }
- @InheritedTest("使用Inherited的註解 method2")
- @InheritedTest2("未使用Inherited的註解 method2")
- public void method2(){
- }
- @InheritedTest("使用Inherited的註解 field")
- @InheritedTest2("未使用Inherited的註解 field")
- public String a;
- }
- /**
- * 子類 只繼承了一個method方法
- * @author crazy
- */
- public class Child extends Parent {
- @Override
- public void method() {
- }
- }
- /**
- * 通過反射進行測試
- * @author crazy
- */
- public class test {
- public static void main(String[] args) throws NoSuchMethodException, SecurityException, NoSuchFieldException {
- Class<Child> clazz = Child.class;
- //對類進行測試
- System.out.println("對類進行測試");
- if(clazz.isAnnotationPresent(InheritedTest.class)){
- System.out.println(clazz.getAnnotation(InheritedTest.class).value());
- }
- if(clazz.isAnnotationPresent(InheritedTest2.class)){
- System.out.println(clazz.getAnnotation(InheritedTest2.class).value());
- }
- System.out.println();
- //對方法 進行測試
- System.out.println("對方法進行測試");
- Method method = clazz.getMethod("method", null);
- if(method.isAnnotationPresent(InheritedTest.class)){
- System.out.println(method.getAnnotation(InheritedTest.class).value());
- }
- if(method.isAnnotationPresent(InheritedTest2.class)){
- System.out.println(method.getAnnotation(InheritedTest2.class).value());
- }
- System.out.println();
- //對方法2 進行測試
- System.out.println("對方法2進行測試");
- Method method2 = clazz.getMethod("method2", null);
- if(method2.isAnnotationPresent(InheritedTest.class)){
- System.out.println(method2.getAnnotation(InheritedTest.class).value());
- }
- if(method2.isAnnotationPresent(InheritedTest2.class)){
- System.out.println(method2.getAnnotation(InheritedTest2.class).value());
- }
- System.out.println();
- //對屬性測試
- System.out.println("對屬性進行測試");
- Field field = clazz.getField("a");
- if(field.isAnnotationPresent(InheritedTest.class)){
- System.out.println(field.getAnnotation(InheritedTest.class).value());
- }
- if(field.isAnnotationPresent(InheritedTest2.class)){
- System.out.println(field.getAnnotation(InheritedTest2.class).value());
- }
- }
- }
下面是輸出結果
- 對類進行測試
- 使用Inherited的註解 class
- 對方法進行測試
- 對方法2進行測試
- 使用Inherited的註解 method2
- 未使用Inherited的註解 method2
- 對屬性進行測試
- 使用Inherited的註解 field
- 未使用Inherited的註解 field
由上可以看出,通過Inherited元註解聲明的自定義註解,在類上使用時,可以被子類繼承,對第一個方法進行測試時,由於子類繼承了父類方法,且兩個都沒有輸出,證明Inherited對方法無效,由方法2可以看出,因爲子類沒有重寫父類方法,所以是直接使用的父類方法,所以兩個都會輸出,同理屬性也是,都會輸出。
所以證明最後結論:通過對註解上使用元註解Inherited聲明出的註解,在使用時用在類上,可以被子類所繼承,對屬性或方法無效。