java 注解中元注解Inherited的使用方法

前言

Inherited是标注元注解的意思的,使用此注解声明出来的自定义注解,如果注解在类上面,子类会自动继承此注解,不带有元注解的自定义注解,子类是无法继承父类上的类注解。谨记,使用@Inherited声明出来的注解,只有在类上使用才会有效,对方法和属性等其他无效。

测试案例

package com.lyj.demo.pojo.annotationTest;

import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * @author 凌兮
 * @date 2021/4/17 8:40
 *
 */
@Retention(RetentionPolicy.RUNTIME)
@Inherited // 代表此注解是元注解,表示此注解在类上使用时,其子类也会继承此注解
public @interface InheritedAnnotation {
    String value();
}
package com.lyj.demo.pojo.annotationTest;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * @author 凌兮
 * @date 2021/4/17 8:43
 * 此注解是一般注解,没有使用@Inherited元注解,表示注解在类上,不会被子类所继承。
 */
@Retention(RetentionPolicy.RUNTIME)
public @interface CommonAnnotation {
    String value();
}


package com.lyj.demo.pojo.annotationTest;

/**
 * @author 凌兮
 * @date 2021/4/17 8:46
 */
@InheritedAnnotation("使用元注解的类")
@CommonAnnotation("未使用元注解的类")
public class ParentClass {


    @InheritedAnnotation("使用元注解的private属性")
    @CommonAnnotation("未使用元注解的private属性")
    private String str;

    @InheritedAnnotation("使用元注解的public属性")
    @CommonAnnotation("未使用元注解的public属性")
    public String str1;

    @InheritedAnnotation("使用元注解的method方法")
    @CommonAnnotation("未使用元注解的method方法")
    public void method(){

    }

    @InheritedAnnotation("使用元注解的method1方法")
    @CommonAnnotation("未使用元注解的method1方法")
    public void method1(){

    }

    @InheritedAnnotation("使用元注解的method2方法")
    @CommonAnnotation("未使用元注解的method2方法")
    private void method2(){

    }
}


package com.lyj.demo.pojo.annotationTest;

/**
 * @author 凌兮
 * @date 2021/4/17 8:48
 */
public class ChildClass extends ParentClass {

    @Override
    public void method1() {

    }
}

// 测试方法:
package com.lyj.demo.pojo.annotationTest;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @author 凌兮
 * @date 2021/4/17 8:45
 */
public class MainClassTest {

    public static void main(String[] args) throws NoSuchMethodException, NoSuchFieldException {
        Class<ChildClass> child = ChildClass.class;
        System.out.println("--------------对类进行测试--------------------");
        // 子类是否带有元注解
        if (child.isAnnotationPresent(InheritedAnnotation.class)) {
            System.out.println(child.getAnnotation(InheritedAnnotation.class).value());
        }
        // 子类是否带有普通注解
        if (child.isAnnotationPresent(CommonAnnotation.class)) {
            System.out.println(child.getAnnotation(CommonAnnotation.class).value());
        }
        System.out.println("----------------对方法进行测试---------------------");
        System.out.println("对method进行测试");
        Method method = child.getMethod("method", null);
        // 子类method1方法是否带有元注解
        if (method.isAnnotationPresent(InheritedAnnotation.class)) {
            System.out.println(method.getAnnotation(InheritedAnnotation.class).value());
        }
        // 子类method1是否带有普通注解
        if (method.isAnnotationPresent(CommonAnnotation.class)) {
            System.out.println(method.getAnnotation(CommonAnnotation.class).value());
        }

        System.out.println("对method1进行测试");
        Method method1 = child.getMethod("method1", null);
        // 子类继承method1方法是否带有元注解
        if (method1.isAnnotationPresent(InheritedAnnotation.class)) {
            System.out.println(method1.getAnnotation(InheritedAnnotation.class).value());
        }
        // 子类继承method1是否带有普通注解
        if (method1.isAnnotationPresent(CommonAnnotation.class)) {
            System.out.println(method1.getAnnotation(CommonAnnotation.class).value());
        }

        System.out.println("---------------------对属性进行测试-----------------------");
        Field field = child.getField("str1");
        // 子类继承父类public属性元注解进行测试
        if (field.isAnnotationPresent(InheritedAnnotation.class)) {
            System.out.println(field.getAnnotation(InheritedAnnotation.class).value());
        }
        // 子类继承父类public属性普通注解进行测试
        if (field.isAnnotationPresent(CommonAnnotation.class)) {
            System.out.println(field.getAnnotation(CommonAnnotation.class).value());
        }
        System.out.println("父类的private属性和方法无法继承,所以子类不存在继承父类私有属性和方法注解情况");


    }
}

运行结果

在这里插入图片描述
通过@Inherited注解声明的自定义注解,

  • 在类上使用时,可以被子类继承,不带有@Inherited的自定义普通注解,子类无法继承父类它。
  • 子类重写父类的方法,是子类自己的方法,所以不输出注解信息
  • 子类继承父类的方法(非重写),所以输出注解的信息,输出的是父类的信息。
  • 子类输出属性的注解信息,是继承父类的属性。私有的无法继承。

总结:

无论如何使用,在使用的时候还是进行自测比较保险。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

凌兮~

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

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

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

打赏作者

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

抵扣说明:

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

余额充值