@Inherited详解以及各种情况,超级详细!!

结论:

当一个被@Inherited注解修饰的注解作用于类上时,其子类也会被加上该注解。
在这里插入图片描述

源码

package java.lang.annotation;

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Inherited {
}

该注解是一个源注解,是jdk中定义的。

各种情况

定义注解

HasInherited注解

package com.example.springtest.Inherited;

import java.lang.annotation.*;

@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface HasInherited {
}

NoInherited

package com.example.springtest.Inherited;

import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface NoInherited {
}

类继承关系(父类注解被@Inherited修饰)


package com.example.springtest.Inherited;

@HasInherited
public class FatherClass_HasInherited {
}


package com.example.springtest.Inherited;

public class ChildH extends FatherClass_HasInherited{
}

@SpringBootTest
class SpringTestApplicationTests {
    @Test
    public void fatherClass_HasInherited(){
        Class<ChildH> childHClass = ChildH.class;
        Annotation[] annotations = childHClass.getAnnotations();
        System.out.println("------------------");
        for (Annotation annotation : annotations) {
            System.out.println("注解名:"+annotation);
        }
        System.out.println("------------------");
    }

}


结果:子类成功获取父类中被@Inherited修饰的注解

类继承关系(父类注解没有@Inherited修饰)

package com.example.springtest.Inherited;

@NoInherited
public interface FatherInterface_NoInherited {
}

package com.example.springtest.Inherited;

public class ChildN extends FatherClass_NoInherited{
}

@SpringBootTest
class SpringTestApplicationTests {

    @Test
    public void fatherClass_NoInherited(){
        Class<ChildN> childN = ChildN.class;
        Annotation[] annotations = childN.getAnnotations();
        System.out.println("------------------");
        for (Annotation annotation : annotations) {
            System.out.println("注解名:"+annotation);
        }
        System.out.println("------------------");
    }
}

在这里插入图片描述

结果:子类成功获取不到父类中没有被@Inherited修饰的注解

类实现接口关系(接口注解有@Inherited修饰)

package com.example.springtest.Inherited;
@HasInherited
public interface FatherInterface_HasInherited {
}

package com.example.springtest.Inherited;

public class ChildHI implements FatherInterface_HasInherited{
}

@SpringBootTest
class SpringTestApplicationTests {

    @Test
    public void fatherInterface_HasInherited(){
        Class<ChildHI> childHI = ChildHI.class;
        Annotation[] annotations = childHI.getAnnotations();
        System.out.println("------------------");
        for (Annotation annotation : annotations) {
            System.out.println("注解名:"+annotation);
        }
        System.out.println("------------------");
    }
}

在这里插入图片描述
结果:子类获取不到父接口中被@Inherited修饰的注解

接口继承接口关系(接口注解有@Inherited修饰)

package com.example.springtest.Inherited;
@HasInherited
public interface FatherInterface_HasInherited {
}

package com.example.springtest.Inherited;

public interface ChildInterface extends FatherInterface_HasInherited{
}

@SpringBootTest
class SpringTestApplicationTests {

    @Test
    public void childInterface(){
        Class<ChildInterface> childInterface = ChildInterface.class;
        Annotation[] annotations = childInterface.getAnnotations();
        System.out.println("------------------");
        for (Annotation annotation : annotations) {
            System.out.println("注解名:"+annotation);
        }
        System.out.println("------------------");
    }

}

在这里插入图片描述
结果:子接口获取不到父接口中被@Inherited修饰的注解

总结

@Inherited修饰的注解只对实现类的继承有效,对接口的实现是无效的。

参考

@inherited 注解详解
@Inherited详解

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值