zoukankan      html  css  js  c++  java
  • @interface __ annotation 子类可以继承到父类上的注解吗--有结论了

     

    博客分类:
     

    作者:赵磊

    博客:http://elf8848.iteye.com

    不了解注解基础知识的请先看《JDK 5 Annotation注解注释自定义注解》

    子类可以继承到父类上的注解吗?

    -----------------------------------------------------------------

    我们知道在编写自定义注解时,可以通过指定@Inherited注解,指明自定义注解是否可以被继承。但实现情况又可细分为多种。

    测试环境如下:

    -----------------------------------------------------------------

    父类的类上和方法上有自定义的注解--MyAnnotation

    子类继承了这个父类,分别:

    子类方法,实现了父类上的抽象方法

    子类方法,继承了父类上的方法

    子类方法,覆盖了父类上的方法

    MyAnnotation自定义注解

    -----------------------------------------------------------------

    Java代码  收藏代码
    1. package test.annotation;  
    2. import java.lang.annotation.Inherited;  
    3. import java.lang.annotation.Retention;  
    4. /** 
    5.  * 自定义注解 
    6.  */  
    7. //@Inherited  //可以被继承  
    8. @Retention(java.lang.annotation.RetentionPolicy.RUNTIME)  //可以通过反射读取注解  
    9. public @interface MyAnnotation {    
    10.     String value();    
    11. }   
     

    父类

    -----------------------------------------------------------------

    Java代码  收藏代码
    1. package test.annotation;  
    2. @MyAnnotation(value = "类名上的注解")  
    3. public abstract class ParentClass {  
    4.   
    5.     @MyAnnotation(value = "父类的abstractMethod方法")  
    6.     public abstract void abstractMethod();  
    7.   
    8.     @MyAnnotation(value = "父类的doExtends方法")  
    9.     public void doExtends() {  
    10.         System.out.println(" ParentClass doExtends ...");  
    11.     }  
    12.       
    13.     @MyAnnotation(value = "父类的doHandle方法")  
    14.     public void doHandle(){  
    15.         System.out.println(" ParentClass doHandle ...");  
    16.     }  
    17. }  
     

    子类

    -----------------------------------------------------------------

    Java代码  收藏代码
    1. package test.annotation;  
    2. public class SubClass extends ParentClass{    
    3.     
    4.     //子类实现父类的抽象方法  
    5.     @Override    
    6.     public void abstractMethod() {    
    7.         System.out.println("子类实现父类的abstractMethod抽象方法");    
    8.     }    
    9.       
    10.     //子类继承父类的doExtends方法  
    11.       
    12.     //子类覆盖父类的doHandle方法  
    13.     @Override    
    14.     public void doHandle(){  
    15.         System.out.println("子类覆盖父类的doHandle方法");   
    16.     }  
    17. }   

    测试类

    -----------------------------------------------------------------

    Java代码  收藏代码
    1. package test.annotation;  
    2.   
    3. import java.lang.reflect.Method;  
    4.   
    5. public class MainTest {  
    6.     public static void main(String[] args) throws SecurityException,  
    7.             NoSuchMethodException {  
    8.   
    9.         Class<SubClass> clazz = SubClass.class;  
    10.   
    11.         if (clazz.isAnnotationPresent(MyAnnotation.class)) {  
    12.             MyAnnotation cla = clazz  
    13.                     .getAnnotation(MyAnnotation.class);  
    14.             System.out.println("子类继承到父类类上Annotation,其信息如下:"+cla.value());  
    15.         } else {  
    16.             System.out.println("子类没有继承到父类类上Annotation");  
    17.         }  
    18.   
    19.         // 实现抽象方法测试  
    20.         Method method = clazz.getMethod("abstractMethod", new Class[] {});  
    21.         if (method.isAnnotationPresent(MyAnnotation.class)) {  
    22.             MyAnnotation ma = method  
    23.                     .getAnnotation(MyAnnotation.class);  
    24.             System.out.println("子类实现父类的abstractMethod抽象方法,继承到父类抽象方法中的Annotation,其信息如下:"+ma.value());  
    25.         } else {  
    26.             System.out.println("子类实现父类的abstractMethod抽象方法,没有继承到父类抽象方法中的Annotation");  
    27.         }  
    28.   
    29.         //覆盖测试  
    30.         Method methodOverride = clazz.getMethod("doExtends", new Class[] {});  
    31.         if (methodOverride.isAnnotationPresent(MyAnnotation.class)) {  
    32.             MyAnnotation ma = methodOverride  
    33.                     .getAnnotation(MyAnnotation.class);  
    34.             System.out  
    35.                     .println("子类继承父类的doExtends方法,继承到父类doExtends方法中的Annotation,其信息如下:"+ma.value());  
    36.         } else {  
    37.             System.out.println("子类继承父类的doExtends方法,没有继承到父类doExtends方法中的Annotation");  
    38.         }  
    39.   
    40.         //继承测试  
    41.         Method method3 = clazz.getMethod("doHandle", new Class[] {});  
    42.         if (method3.isAnnotationPresent(MyAnnotation.class)) {  
    43.             MyAnnotation ma = method3  
    44.                     .getAnnotation(MyAnnotation.class);  
    45.             System.out  
    46.                     .println("子类覆盖父类的doHandle方法,继承到父类doHandle方法中的Annotation,其信息如下:"+ma.value());  
    47.         } else {  
    48.             System.out.println("子类覆盖父类的doHandle方法,没有继承到父类doHandle方法中的Annotation");  
    49.         }  
    50.     }  
    51. }  
      

    编写自定义注解时未写@Inherited的运行结果

    -----------------------------------------------------------------

    子类没有继承到父类类上Annotation

    子类实现父类的abstractMethod抽象方法,没有继承到父类抽象方法中的Annotation

    子类继承父类的doExtends方法,继承到父类doExtends方法中的Annotation,其信息如下:父类的doExtends方法

    子类覆盖父类的doHandle方法,没有继承到父类doHandle方法中的Annotation

    编写自定义注解时写了@Inherited的运行结果

    -----------------------------------------------------------------

    子类继承到父类类上Annotation,其信息如下:类名上的注解

    子类实现父类的abstractMethod抽象方法,没有继承到父类抽象方法中的Annotation

    子类继承父类的doExtends方法,继承到父类doExtends方法中的Annotation,其信息如下:父类的doExtends方法

    子类覆盖父类的doHandle方法,没有继承到父类doHandle方法中的Annotation

    结论

    -----------------------------------------------------------------

    父类的类上和方法上有自定义的注解,

    子类继承了这个父类,的情况下。

      编写自定义注解时未写@Inherited的运行结果: 编写自定义注解时写了@Inherited的运行结果:
    子类的类上能否继承到父类的类上的注解?
    子类方法,实现了父类上的抽象方法,这个方法能否继承到注解?
    子类方法,继承了父类上的方法,这个方法能否继承到注解?
    子类方法,覆盖了父类上的方法,这个方法能否继承到注解?

    我们知道在编写自定义注解时,可以通过指定@Inherited注解,指明自定义注解是否可以被继承。

    通过测试结果来看,@Inherited 只是可控制 对类名上注解是否可以被继承。不能控制方法上的注解是否可以被继承。

    附注

    -----------------------------------------------------------------

    Spring 实现事务的注解@Transactional 是可以被继承的,

    通过查看它的源码可以看到@Inherited。

     _______________________ _______________________ _______________________

    只能继承父类(普通类,抽象类)的注解, 接口的都不行
    并且只能父类的class级别的注解才能继承:@Target(value=
    ElementType.Type,
    ElementType.TYPE_PARAMETER )

     _______________________ _______________________ _______________________

      

     
  • 相关阅读:
    Best Time to Buy and Sell Stock
    Remove Nth Node From End of List
    Unique Paths
    Swap Nodes in Pairs
    Convert Sorted Array to Binary Search Tree
    Populating Next Right Pointers in Each Node
    Maximum Subarray
    Climbing Stairs
    Unique Binary Search Trees
    Remove Duplicates from Sorted Array
  • 原文地址:https://www.cnblogs.com/kelelipeng/p/11864804.html
Copyright © 2011-2022 走看看