20230710 java.lang.annotation.Inherited

介绍

  • java.lang.annotation.Inherited
  • 声明
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Inherited
  • 元注解
  • 标记注解
  • 指明当这个注解应用于一个类的时候,能够自动被它的子类继承

代码示例

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


@Persistent
public class Employee {
}


public class Manager extends Employee{
}


public class Main {
    public static void main(String[] args) {
        Class<Manager> managerClass = Manager.class;
        Annotation[] annotations = managerClass.getAnnotations();
        System.out.println(Arrays.toString(annotations));   // [@v2ch08.persist.Persistent()]

        Annotation[] declaredAnnotations = managerClass.getDeclaredAnnotations();
        System.out.println(Arrays.toString(declaredAnnotations));   // []

        Class<Employee> employeeClass = Employee.class;
        System.out.println(Arrays.toString(employeeClass.getAnnotations()));    // [@v2ch08.persist.Persistent()]
        System.out.println(Arrays.toString(employeeClass.getDeclaredAnnotations()));    // [@v2ch08.persist.Persistent()]
    }
}
posted @ 2023-08-24 10:25  流星<。)#)))≦  阅读(5)  评论(0编辑  收藏  举报