关于java 注解中元注解Inherited的使用详解

关于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声明出的注解,在使用时用在类上,可以被子类所继承,对属性或方法无效。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值