@Inherit注解与注解的继承

本文通过实例详细探讨了Java中注解的继承性问题,包括类注解与方法注解的不同继承行为,以及@Inherited元注解的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

引言

父类上的注解能否被子类继承,怎样才能被子类继承?父类方法上的注解呢?

验证

先定义一个注解

@Inherited
@Retention(RUNTIME)   // 可以通过反射读取注解
public @interface MyInheritedAnnotation {
    String value() ;
}

然后定义父类,类上和方法上都标注有MyInheritedAnnotation注解

@MyInheritedAnnotation("father class")
public abstract class Father {
    @MyInheritedAnnotation("覆盖")
    public void f1() {}

    @MyInheritedAnnotation("继承")
    public void f2() {}

    @MyInheritedAnnotation("子类实现抽象方法")
    public abstract void abstractFunction();
}

再定义子类,实现父类的抽象方法abstractFunction,覆盖父类的f1,继承父类的f2

public class Son extends Father {
    @Override
    public void f1() {}

    @Override
    public void abstractFunction() {}
}

最后写一个测试类:

public class InheritedTest {
    public static void main(String[] args) {
        System.out.println("************** Father ****************");
        getClassAnnotations(Father.class);
        Method[] methods = Father.class.getMethods();
        for (int i = 0; i < methods.length; i++) {
            getMethodAnnotations(methods[i]);
        }

        System.out.println("**************** Son *****************");
        getClassAnnotations(Son.class);
        methods = Son.class.getMethods();
        for (int i = 0; i < methods.length; i++) {
            getMethodAnnotations(methods[i]);
        }
    }

    public static void getClassAnnotations(Class clazz) {
        Annotation[] annotations = clazz.getAnnotations();
        for (int i = 0; i < annotations.length; i++) {
            System.out.println("类:" + clazz.getName() + "上注解:\t" +
                   (((MyInheritedAnnotation) annotations[i]).value()));
        }
    }

    public static void getMethodAnnotations(Method method) {
        Annotation[] annotations = method.getAnnotations();
        for (int i = 0; i < annotations.length; i++) {
            System.out.println("方法:" + method.getName() + "上注解:\t" +
                   (((MyInheritedAnnotation) annotations[i]).value()));
        }
    }

}

结果:

************** Father ****************
类:com.seaxll.annotation.inherit.Father上注解:	father class
方法:abstractFunction上注解:	子类实现抽象方法
方法:f2上注解:	继承
方法:f1上注解:	覆盖
**************** Son *****************
类:com.seaxll.annotation.inherit.Son上注解:	father class
方法:f2上注解:	继承

Process finished with exit code 0

然后注释掉@Inherited注解

// @Inherited
@Retention(RUNTIME)   // 可以通过反射读取注解
public @interface MyInheritedAnnotation {
    String value() ;
}

再运行测试代码:

************** Father ****************
类:com.seaxll.annotation.inherit.Father上注解:	father class
方法:f1上注解:	覆盖
方法:f2上注解:	继承
方法:abstractFunction上注解:	子类实现抽象方法
**************** Son *****************
方法:f2上注解:	继承

Process finished with exit code 0

结论

从上面的执行结果可以看出:

  • 对于类:当取消@Inherited注解的时候,子类没有继承父类类上的MyInheritedAnnotation,即:子类只能继承父类上有@Inherited注解的注解;
  • 对于方法:
    • 普通继承:无论注解有不有@Inherited,子类的方法都能继承父类方法上的注解
    • 抽象实现:无法继承父类方法上的注解
    • 方法覆盖:无法继承父类方法上的注解

即:

  • @Inherited注解只影响类上注解的继承关系;不影响方法上注解的继承关系
  • 覆盖和抽象方法的实现是覆盖和覆盖父类的注解

父类的类上和方法上有自定义的注解,子类继承了这个父类,的情况下,有以下规则:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

非理性地界生物

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值