@Inherited

2024-05-12 16:32
文章标签 inherited

本文主要是介绍@Inherited,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

@Inherited是一个标识,用来修饰注解
作用:如果一个类用上了@Inherited修饰的注解,那么其子类也会继承这个注解

注意:

接口用上个@Inherited修饰的注解,其实现类不会继承这个注解
父类的方法用了@Inherited修饰的注解,子类也不会继承这个注解
当用了@Inherited修饰的注解的@Retention是RetentionPolicy.RUNTIME,则增强了继承性,在反射中可以获取得到

代码演示:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface ATable {
    public String name() default "";
}


@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface BTable {
    public String name() default "";
}

//作为父类
@ATable
public class Super {
    private int superx;
    public int supery;

    public Super() {
    }
    //私有
    private int superX(){
        return 0;
    }
    //公有
    public int superY(){
        return 0;
    }

}

@BTable
public class Sub extends Super {
    private int subx;
    public int suby;

    private Sub() {
    }

    public Sub(int i) {
    }

    //私有
    private int subX() {
        return 0;
    }
    //公有
    public int subY() {
        return 0;
    }

}

class TestMain {
    public static void main(String[] args) { 
        Class<Sub> clazz = Sub.class;
        System.out.println("============================AnnotatedElement===========================");
        System.out.println(Arrays.toString(clazz.getAnnotations()));    //获取自身和父亲的注解。如果@ATable未加@Inherited修饰,则获取的只是自身的注解而无法获取父亲的注解。
        System.out.println("------------------");
    }
}
 

以下是对反射的拓展,与上文无关:

class TestMain {
    public static void main(String[] args) {
        Class<Sub> clazz = Sub.class;
        System.out.println("============================Field===========================");
        System.out.println(Arrays.toString(clazz.getFields())); // 自身和父亲的公有字段
        System.out.println("------------------");
        System.out.println(Arrays.toString(clazz.getDeclaredFields()));  //自身所有字段
        System.out.println("============================Method===========================");
        System.out.println(Arrays.toString(clazz.getMethods()));   //自身和父亲的公有方法
        System.out.println("------------------");
        System.out.println(Arrays.toString(clazz.getDeclaredMethods()));// 自身所有方法
        System.out.println("============================Constructor===========================");
        System.out.println(Arrays.toString(clazz.getConstructors()));   //自身公有的构造方法
        System.out.println("------------------");
        System.out.println(Arrays.toString(clazz.getDeclaredConstructors()));   //自身的所有构造方法
        System.out.println("============================AnnotatedElement===========================");
        System.out.println(Arrays.toString(clazz.getAnnotations()));    //获取自身和父亲的注解
        System.out.println("------------------");
        System.out.println(Arrays.toString(clazz.getDeclaredAnnotations()));  //只获取自身的注解
        System.out.println("------------------");
    }
}

通过代码的结果得知:子类继承了父类(由于继承特性,子类会拥有父类的公有一切),在通过反射获取子类所有公有字段/方法/构造器的时候,会获取得到自身和父亲的所有public字段/方法/构造器,而通过反射获取所有任何字段/方法/构造器的时候,只能得到自身的所有任何访问权限修饰符的字段/方法/构造器,不会得到父类的任何字段/方法/构造器。然注解不一样,只有当父类的注解中用@Inherited修饰,子类的getAnnotations()才能获取得到父亲的注解以及自身的注解,而getDeclaredAnnotations()只会获取自身的注解,无论如何都不会获取父亲的注解。

还有下面几个注解经常和Inherited一起出现

@Target:注解的作用目标

@Target(ElementType.TYPE)——接口、类、枚举、注解
@Target(ElementType.FIELD)——字段、枚举的常量
@Target(ElementType.METHOD)——方法
@Target(ElementType.PARAMETER)——方法参数
@Target(ElementType.CONSTRUCTOR) ——构造函数
@Target(ElementType.LOCAL_VARIABLE)——局部变量
@Target(ElementType.ANNOTATION_TYPE)——注解
@Target(ElementType.PACKAGE)——包

@Retention:注解的保留位置

RetentionPolicy.SOURCE:这种类型的Annotations只在源代码级别保留,编译时就会被忽略,在class字节码文件中不包含。
RetentionPolicy.CLASS:这种类型的Annotations编译时被保留,默认的保留策略,在class文件中存在,但JVM将会忽略,运行时无法获得。
RetentionPolicy.RUNTIME:这种类型的Annotations将被JVM保留,所以他们能在运行时被JVM或其他使用反射机制的代码所读取和使用。
@Document:说明该注解将被包含在javadoc中
————————————————
版权声明:本文为CSDN博主「fengcai0123」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/fengcai0123/article/details/90544338

这篇关于@Inherited的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/983146

相关文章

「Debug R」报错unable to find an inherited method for function是如何产生的

在一个群里看到这样一条报错,截图如下: 报错信息 当然这种问题解决起来也很快,无非就是把报错信息复制出来放在搜索引擎上,只不过你要挑选合适的搜索引擎。 百度 谷歌 必应 解决方案就是用dplyr::select。 虽然报错解决了,但是我还想着要重复出这个报错。因为只有能重复出报错,才能证明你不是运气好才解

【Java】@Inherited 注解

Java里面有四种元注解,@Inherited便是其中之一,其作用在java文档中写的很清楚了: * Indicates that an annotation type is automatically inherited. If* an Inherited meta-annotation is present on an annotation type* declaration, and

@inherited 注解详解

1【问题引出】 在 Springboot 项目中,通常会有一个启动类,而启动类中通常会有一个名为  @SpringBootApplication 的注解(如下图所示),而此注解就是 Springboot 项目启动类的核心注解。 我们打开 @SpringBootApplication 注解源码,可以看到,在源码中有一个 @Inherited 的注解,那么这个注解,它的作用是什么呢?

java元注解_java元注解@Inherited的使用详解

1.先看源码文档 /** * Indicates that an annotation type is automatically inherited. If * an Inherited meta-annotation is present on an annotation type * declaration, and the user queries the annotation type

‘XXX‘has sample time [0, 1]. Only constant (inf) or inherited (-1) sample times are allowed in‘XXX‘

学习matlab/simulink时使用Memory模块和Trigger模块 报错:'XXX'has sample time [0, 1]. Only constant (inf) or inherited (-1) sample times are allowed in 'XXX' 说明memory模块设置不对 双击memory模块 解决办法:勾选Inherit s

CocoaPods:“Use the `$(inherited)` flag” or “Remove the build settings from the target”

问题背景解决方法总结 阅读之前注意: 本文阅读建议用时:5min 问题背景 CocoaPods导库的时候,出现了如下情况: [!] The `project [Debug]` target overrides the `PODS_CONFIGURATION_BUILD_DIR` build setting defined in `Pods/Target Support File

Java注解之Retention、Documented、Inherited介绍

Retention注解 Retention(保留)注解说明,这种类型的注解会被保留到那个阶段. 有三个值: 1.RetentionPolicy.SOURCE —— 这种类型的Annotations只在源代码级别保留,编译时就会被忽略 2.RetentionPolicy.CLASS —— 这种类型的Annotations编译时被保留,在class文件中存在,但JVM将会忽略 3.Retenti

ZOJ 1516 Uncle Tom's Inherited Land (二分图匹配)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1516 Your old uncle Tom inherited a piece of land from his great-great-uncle. Originally, the property had been in the shape of a

@Inherited注解类

包名package java.lang.annotation; 作用         指示批注类型是自动继承的。如果注释类型声明上存在Inherited元注释,并且用户在类声明上查询注释类型,并且该类声明没有该类型的注释,则将自动查询该类的超类的注释类型。将重复此过程,直到找到此类型的注释,或者到达类层次结构(Object)的顶部。如果没有超类具有此类型的注释,那么查询将指示有问题的类没有此类