Java注解之@Inherited

来源:互联网 发布:js 获取div value值 编辑:程序博客网 时间:2024/05/17 22:41

@Inherited:原注解,用于修饰自定义注解的metadata

使用此注解修饰自定义注解时,效果是可以让自定义注解有继承特性,当将带有@Inherited的注解使用在A类名上(B的父类),则subClass B类自动继承该注解。

注:将带有@Inherited的注解使用在类的方法,属性则是无效的。即使用在父类的方法和属性上,子类不会继承该注解。大家可能晕了,这里说的继承是superClass-A 类或者方法上带有注解,subClass-B上不带注解,这时当B extends A 时,能不能在B上自动带上这个带有@Inherited的注解。

public class AnotationTest {    public static void main(String[] args) throws NoSuchMethodException, SecurityException, NoSuchFieldException {        //子类class        Class<Child> clazz = Child.class;        //对类上的注解进行验证进行测试        if (clazz.isAnnotationPresent(InheritedTest.class)) {            System.out.println(clazz.getAnnotation(InheritedTest.class).value());        }        if (clazz.isAnnotationPresent(NoInheritedTest.class)) {            System.out.println(clazz.getAnnotation(NoInheritedTest.class).value());        }        //对子类需要覆盖的方法 进行测试        Method method = clazz.getMethod("methodNeedOverrid", null);        if (method.isAnnotationPresent(InheritedTest.class)) {            System.out.println(method.getAnnotation(InheritedTest.class).value());        }        if (method.isAnnotationPresent(NoInheritedTest.class)) {            System.out.println(method.getAnnotation(NoInheritedTest.class).value());        }        //对子类不需要覆盖的方法 进行测试,说白了此方法是父类的,肯定有注解,不管是带与不带@Inherited        Method method2 = clazz.getMethod("methodNotNeedOverrid", null);        if (method2.isAnnotationPresent(InheritedTest.class)) {            System.out.println(method2.getAnnotation(InheritedTest.class).value());        }        if (method2.isAnnotationPresent(NoInheritedTest.class)) {            System.out.println(method2.getAnnotation(NoInheritedTest.class).value());        }        //对继承的属性测试,属性也是父类的也肯定带        Field field = clazz.getField("fieldString");        if (field.isAnnotationPresent(InheritedTest.class)) {            System.out.println(field.getAnnotation(InheritedTest.class).value());        }        if (field.isAnnotationPresent(NoInheritedTest.class)) {            System.out.println(field.getAnnotation(NoInheritedTest.class).value());        }    }}// 无任何注解,如果在主程度中我们检测到Child中出现了注解,则说明父类的注解继承到子类了class Child extends Parent {    public String fieldString;    @Override    public void methodNeedOverrid() {    }}@InheritedTest("使用Inherited的注解类")@NoInheritedTest("未使用Inherited的注解类")class Parent {    @InheritedTest("方法-子类需要覆盖且使用Inherited的注解")    @NoInheritedTest("方法-子类需要覆盖且未使用Inherited的注解")    public void methodNeedOverrid() {    }    @InheritedTest("方法-子类不需要覆盖且使用Inherited的注解")    @NoInheritedTest("方法-子类不需要覆盖且未使用Inherited的注解")    public void methodNotNeedOverrid() {    }    @InheritedTest("属性-在父类属性上使用Inherited的注解")    @NoInheritedTest("属性-在父类属性上未使用Inherited的注解")    public String fieldString;}
@Retention(RetentionPolicy.RUNTIME)@Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD})@Inheritedpublic @interface InheritedTest {    String value() default "lei";}
@Retention(RetentionPolicy.RUNTIME)public @interface NoInheritedTest {    String value() default "no inheritated";}

1 0
原创粉丝点击