java注解的继承关系

在编写自定义注解时,可以通过指定@Inherited注解,指明自定义注解是否可以被继承,接口的处理方式是不一样的。

继承的方式:

测试环境如下:

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

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

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

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

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

测试代码:

public class TestInherited {

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

    @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 ...");
        }
    }

    public class SubClass extends ParentClass {

        //子类实现父类的抽象方法
        @Override
        public void abstractMethod() {
            System.out.println("子类实现父类的abstractMethod抽象方法");
        }

        //子类继承父类的doExtends方法

        //子类覆盖父类的doHandle方法
        @Override
        public void doHandle() {
            System.out.println("子类覆盖父类的doHandle方法");
        }
    }

    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 只是可控制 对类名上注解是否可以被继承。不能控制方法上的注解是否可以被继承

接口的方式

public class TestInheritedInf {

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

    @MyAnnotation(value = "接口上的注解")
    public interface ParentInf {

        @MyAnnotation(value = "接口的abstractMethod方法")
        void abstractMethod();
    }

    public class SubImpl implements ParentInf {

        //子类实现父类的抽象方法
        @Override
        public void abstractMethod() {
            System.out.println("子类实现接口的abstractMethod抽象方法");
        }
    }

    public static void main(String[] args) throws Exception {

        Class<SubImpl> clazz = SubImpl.class;
        
        Class[] i = clazz.getInterfaces();
        for (Class clz : i) {
            if(clz.isAnnotationPresent(MyAnnotation.class)){
                MyAnnotation w = (MyAnnotation)clz.getAnnotation(MyAnnotation.class);
                System.out.println("value:" + w.value());
            }

            Method[] methods = clz.getMethods();
            for (Method method : methods) {
                if (method.isAnnotationPresent(MyAnnotation.class)) {
                    MyAnnotation w = method.getAnnotation(MyAnnotation.class);
                    System.out.println("value:" + w.value());
                }
            }
        }
    }
}

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

value:接口上的注解
value:接口的abstractMethod方法

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

value:接口上的注解
value:接口的abstractMethod方法

结论:实现类不管加不加@Inherited都能读取到定义接口的接口或者方法上面的注解

如果实现的多个接口声明了同样的方法,实现类只要实现一次就可以了,然后每个接口都能调用。

在spring中比如GetMapping等注解,不仅可以实现接口读取,也可以被继承读取,因为通过spring框架通过反射和代理处理了的

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值