Java注解的继承

  • 类、方法、参数上的注解都可以被继承
  • 如果方法被重写了,方法上和参数上的 Annotation 将不会被继承,但是类的注解还是会被继承
  • Annotation 的继承不能应用在接口上
  • 类、方法、参数上的注解都可以被继承
@Retention(RetentionPolicy.RUNTIME)
public @interface InheritedTest {
    String hello();
}

@InheritedTest(hello = "bob")
public class InheritedParent {

    @InheritedTest(hello = "smith")
    public void doSomething(@InheritedTest(hello = "param") String param){
        System.out.println("Parent do something!");
    }
}

public class InheritedChild extends InheritedParent {


}

测试

//类注解的继承
@Test
public void test01(){
    Class<InheritedChild> inheritedChildClass = InheritedChild.class;
    if(inheritedChildClass.isAnnotationPresent(InheritedTest.class)){
        InheritedTest annotation = inheritedChildClass.getAnnotation(InheritedTest.class);
        System.out.println(annotation.hello());
    }
}

//方法注解的继承
@Test
public void test02() throws NoSuchMethodException {
    Class<InheritedChild> inheritedChildClass = InheritedChild.class;
    Method doSomething = inheritedChildClass.getMethod("doSomething", new Class[]{});
    if (doSomething.isAnnotationPresent(InheritedTest.class)) {
        InheritedTest annotation = doSomething.getAnnotation(InheritedTest.class);
        System.out.println(annotation.hello());
    }
}

//参数注解的继承
@Test
public void test03() throws NoSuchMethodException {
    Class<InheritedChild> inheritedTestClass = InheritedChild.class;
    Method doSomething = inheritedTestClass.getMethod("doSomething", new Class[]{String.class});
    Parameter[] parameters = doSomething.getParameters();
    for (Parameter parameter : parameters) {
        if (parameter.isAnnotationPresent(InheritedTest.class)) {
            InheritedTest annotation = parameter.getAnnotation(InheritedTest.class);
            System.out.println(annotation.hello());
        }
    }
}

如果方法被重写了,方法上和参数上的 Annotation 将不会被继承,但是类的注解还是会被继承

public class InheritedChild extends InheritedParent {

    public void doSomething(String param){
        System.out.println("Parent do something!");
    }

}

测试

//方法注解的继承
@Test
public void test02() throws NoSuchMethodException {
    Class<InheritedChild> inheritedChildClass = InheritedChild.class;
    Method doSomething = inheritedChildClass.getMethod("doSomething", new Class[]{});
    if (doSomething.isAnnotationPresent(InheritedTest.class)) {
        InheritedTest annotation = doSomething.getAnnotation(InheritedTest.class);
        System.out.println(annotation.hello());
    }
}

Annotation 的继承不能应用在接口上

@InheritedTest(hello = "interface")
public interface InheritedParent02 {
}

public class InheritedChild02 implements InheritedParent02{
}

//接口注解的继承
@Test
public void test04(){
    Class<InheritedChild02> inheritedChild02Class = InheritedChild02.class;
    if (inheritedChild02Class.isAnnotationPresent(InheritedTest.class)) {
        InheritedTest annotation = inheritedChild02Class.getAnnotation(InheritedTest.class);
        System.out.println(annotation.hello());
    }
}

参考:https://www.iteye.com/blog/yahaitt-144565

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值