关于子类可否继承到父类上的注解

附注


Spring 实现事务的注解@Transactional 是可以被继承的,通过查看它的源码可以看到@Inherited。

正文


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


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

测试环境如下:


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

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

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

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

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

MyAnnotation自定义注解


package test.annotation;  
import java.lang.annotation.Inherited;  
import java.lang.annotation.Retention;  
/** 
 * 自定义注解 
 */  
//@Inherited  //可以被继承  
@Retention(java.lang.annotation.RetentionPolicy.RUNTIME)  //可以通过反射读取注解  
public @interface MyAnnotation {    
    String value();    
}   

父类


package test.annotation;  
@MyAnnotation(value = "类名上的注解")  
public abstract class ParentClass {  
  
    @MyAnnotation(value = "父类的abstractMethod方法")  
    public abstract void abstractMethod();  
  
    @MyAnnotation(value = "父类的doExtends方法")  
    public void doExtends() {  
        System.out.println(" ParentClass doExtends ...");  
    }  
      
    @MyAnnotation(value = "父类的doHandle方法")  
    public void doHandle(){  
        System.out.println(" ParentClass doHandle ...");  
    }  
}  

子类


package test.annotation;  
public class SubClass extends ParentClass{    
    
    //子类实现父类的抽象方法  
    @Override    
    public void abstractMethod() {    
        System.out.println("子类实现父类的abstractMethod抽象方法");    
    }    
      
    //子类继承父类的doExtends方法  
      
    //子类覆盖父类的doHandle方法  
    @Override    
    public void doHandle(){  
        System.out.println("子类覆盖父类的doHandle方法");   
    }  
}   

测试类


package test.annotation;  
  
import java.lang.reflect.Method;  
  
public class MainTest {  
    public static void main(String[] args) throws SecurityException,  
            NoSuchMethodException {  
  
        Class<SubClass> clazz = SubClass.class;  
  
        if (clazz.isAnnotationPresent(MyAnnotation.class)) {  
            MyAnnotation cla = clazz  
                    .getAnnotation(MyAnnotation.class);  
            System.out.println("子类继承到父类类上Annotation,其信息如下:"+cla.value());  
        } else {  
            System.out.println("子类没有继承到父类类上Annotation");  
        }  
  
        // 实现抽象方法测试  
        Method method = clazz.getMethod("abstractMethod", new Class[] {});  
        if (method.isAnnotationPresent(MyAnnotation.class)) {  
            MyAnnotation ma = method  
                    .getAnnotation(MyAnnotation.class);  
            System.out.println("子类实现父类的abstractMethod抽象方法,继承到父类抽象方法中的Annotation,其信息如下:"+ma.value());  
        } else {  
            System.out.println("子类实现父类的abstractMethod抽象方法,没有继承到父类抽象方法中的Annotation");  
        }  
  
        //覆盖测试  
        Method methodOverride = clazz.getMethod("doExtends", new Class[] {});  
        if (methodOverride.isAnnotationPresent(MyAnnotation.class)) {  
            MyAnnotation ma = methodOverride  
                    .getAnnotation(MyAnnotation.class);  
            System.out  
                    .println("子类继承父类的doExtends方法,继承到父类doExtends方法中的Annotation,其信息如下:"+ma.value());  
        } else {  
            System.out.println("子类继承父类的doExtends方法,没有继承到父类doExtends方法中的Annotation");  
        }  
  
        //继承测试  
        Method method3 = clazz.getMethod("doHandle", new Class[] {});  
        if (method3.isAnnotationPresent(MyAnnotation.class)) {  
            MyAnnotation ma = method3  
                    .getAnnotation(MyAnnotation.class);  
            System.out  
                    .println("子类覆盖父类的doHandle方法,继承到父类doHandle方法中的Annotation,其信息如下:"+ma.value());  
        } else {  
            System.out.println("子类覆盖父类的doHandle方法,没有继承到父类doHandle方法中的Annotation");  
        }  
    }  
}  

编写自定义注解时未写@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 只是可控制 对类名上注解是否可以被继承。不能控制方法上的注解是否可以被继承。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值