ITeye博客

Java中@Inherited注解的运用

xiangdefei · 2011-05-14 15:13 · 编程语言 · Java · Markdown

我们自定义注解(Annotation)时,把自定义的注解标注在父类上不会被子类所继承,但是我们可以在定义注解时给我们自定义的注解标注一个@Inherited注解来实现注解继承。 自定义的注解代码如下: [code="java"] package com.xdf.annotation; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; @Inherited @Retention(java.lang.annotation.RetentionPolicy.RUNTIME) public @interface InheritedAnnotation { String value(); } [/code] 接着定义一个抽象父类 代码如下: [code="java"] package com.xdf.annotation; public abstract class AbstractParent { @InheritedAnnotation(value = "parent abstractMethod ") public abstract void abstractMethod(); @InheritedAnnotation(value = "Parent's doExtends") public void doExtends() { System.out.println(" AbstractParent doExtends ..."); } } [/code] 我们把自定义的注解父类里的方法上。 接着定义一个继承抽象父类(AbstractParent)的子类 代码如下: [code="java"] package com.xdf.annotation; public class SubClassImpl extends AbstractParent{ @Override public void abstractMethod() { System.out.println("子类实现抽象父类的抽象方法"); } } [/code] 在子类中实现了abstractMethod抽象方法,没有重写doExtends方法。 测试类代码如下: [code="java"] package com.xdf.annotation; import java.lang.reflect.Method; public class InheritedAnnotationTest { public static void main(String[] args) throws SecurityException, NoSuchMethodException { Class clazz=SubClassImpl.class; //abstractMethod Method method = clazz.getMethod("abstractMethod", new Class[]{}); if(method.isAnnotationPresent(InheritedAnnotation.class)){ InheritedAnnotation ma = method.getAnnotation(InheritedAnnotation.class); System.out.println("子类实现的抽象方法继承到父类抽象方法中的Annotation,其信息如下:"); System.out.println(ma.value()); }else{ System.out.println("子类实现的抽象方法没有继承到父类抽象方法中的Annotation"); } Method methodOverride = clazz.getMethod("doExtends", new Class[]{}); if(methodOverride.isAnnotationPresent(InheritedAnnotation.class)){ InheritedAnnotation ma = methodOverride.getAnnotation(InheritedAnnotation.class); System.out.println("子类doExtends方法继承到父类doExtends方法中的Annotation,其信息如下:"); System.out.println(ma.value()); }else{ System.out.println("子类doExtends方法没有继承到父类doExtends方法中的Annotation"); } } } [/code] 运行结果如下: [code="java"] 子类实现的抽象方法没有继承到父类抽象方法中的Annotation 子类doExtends方法继承到父类doExtends方法中的Annotation,其信息如下: Parent's doExtends [/code] 从以上代码运行的结果可以得到如下结论: 1.如果子类继承父类,并且重写了父类中的带有注解的方法,那么父类方法上的注解是不会被子类继承的。 2.如果子类继承父类,但是没有重写父类中带有注解的方法,那么父类方法上的注解会被子类继承,就是说在子类中可以得到父类方法上的注解。 但是.....但是....当我把自定义的注解上的@Inherited注解去掉运行,结果还是一样,也就是说这个@Inherited注解根本没有起作用。这是什么神马情况呢? 接着我把没有标注@Inherited注解的自定义的注解标注在类级别上(不是方法级别上),把抽象父类改成下面这样: [code="java"] package com.xdf.annotation; @InheritedAnnotation(value="parent") //把自定义注解标注在父类上 public abstract class AbstractParent { @InheritedAnnotation(value = "parent abstractMethod ") public abstract void abstractMethod(); @InheritedAnnotation(value = "Parent's doExtends") public void doExtends() { System.out.println(" AbstractParent doExtends ..."); } } [/code] 然后在测试类的main方法里加了如下测试代码: [code="java"] if(clazz.isAnnotationPresent(InheritedAnnotation.class)){ InheritedAnnotation cla = clazz.getAnnotation(InheritedAnnotation.class); System.out.println("子类继承到父类类上Annotation,其信息如下:"); System.out.println(cla.value()); }else{ System.out.println("子类没有继承到父类类上Annotation"); } [/code] 这是出现情况了,运行main方法得到结果: [code="java"] 子类实现的抽象方法没有继承到父类抽象方法中的Annotation 子类doExtends方法继承到父类doExtends方法中的Annotation,其信息如下: Parent's doExtends 子类没有继承到父类类上Annotation [/code] 从运行结果中可以发现子类并没有继承父类类级别的注解,于是我又把@Inherited注解标注在自定义注解上,然后运行一下,得到如下结果: [code="java"] 子类实现的抽象方法没有继承到父类抽象方法中的Annotation 子类doExtends方法继承到父类doExtends方法中的Annotation,其信息如下: Parent's doExtends 子类继承到父类类上Annotation,其信息如下: parent[/code] 注意运行结果,子类继承了父类类级别的注解了。 这说明什么呢? 说明这种标有@Inherited注解的自定义的注解运用到类级别上和方法级别上是不一样的,如果把标有@Inherited注解的自宝义的注解标注在类级别上,子类则可以继承父类类级别的注解,反之,则不行。

评论(2)

  1. #2 楼 · lemon_gogogo · 2015-08-23 23:45

    看看这个http://blog.csdn.net/lemon89/article/details/47836783

  2. #1 楼 · lemon_gogogo · 2015-08-23 23:40

    赶紧删了把。。错了 :?: This annotation applies only to class declarations.

评论功能维护中,当前仅展示历史评论。