父类上的注解能被子类继承吗,接口上面的注解呢(转)

原文:

https://www.iteye.com/blog/wzhw2008-1723136

https://www.cnblogs.com/grasp/p/11362118.html

 

我们知道在编写自定义注解时,可以通过指定@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框架通过反射和代理处理了的

posted @ 2021-01-14 23:31  奋斗终生  Views(1199)  Comments(0Edit  收藏  举报