java元注解@Inherited怎么用

发布时间:2021-09-27 14:59:23 作者:小新
来源:亿速云 阅读:110

这篇文章将为大家详细讲解有关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 on a class * declaration, and the class declaration has no annotation for this type, * then the class's superclass will automatically be queried for the * annotation type. This process will be repeated until an annotation for this * type is found, or the top of the class hierarchy (Object) * is reached. If no superclass has an annotation for this type, then * the query will indicate that the class in question has no such annotation. * * <p>Note that this meta-annotation type has no effect if the annotated * type is used to annotate anything other than a class. Note also * that this meta-annotation only causes annotations to be inherited * from superclasses; annotations on implemented interfaces have no * effect. * * @author Joshua Bloch * @since 1.5 * @jls 9.6.3.3 @Inherited */@Documented@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.ANNOTATION_TYPE)public @interface Inherited {}

上述代码注释部分可以用谷歌翻译大法大致意思是

表示注释类型自动继承。 如果在注释类型声明中存在继承的元注释,并且用户在类声明上查询注释类型,并且类声明没有此类型的注释,则该类的超类将自动查询注释类型。 将重复此过程,直到找到此类型的注释,或者达到类层次结构(Object)的顶部。 如果没有超类具有此类型的注释,则查询将指示所讨论的类没有这样的注释。

请注意,如果使用注释类型来注释除类之外的任何内容,则此元注释类型不起作用。 还要注意,这个元注释只会导致从超类继承注释; 已实现的接口上的注释无效。

通过上述描述可知,使用该注解的注解父类的子类可以继承父类的注解。

2.代码测试

2.1拥有@Inherited注解

@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Inheritedpublic @interface InheritedTest {  String value();}

@InheritedTest("拥有Inherited")public class Person {  public void method(){  }  public void method2(){  }}

public class Student extends Person {}

测试:

public class TestInherited {  public static void main(String[] args) {    Class<Student> studentClass = Student.class;    if (studentClass.isAnnotationPresent(InheritedTest.class)){      System.out.println(studentClass.getAnnotation(InheritedTest.class).value());    }  }}

输出:

2.2未拥有@Inherited注解

@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)public @interface IsNotInherited {  String value();}

@IsNotInherited("未拥有Inherited")public class Person {  public void method(){  }  public void method2(){  }}

public class Student extends Person {}

测试:

public class TestInherited {  public static void main(String[] args) {    Class<Student> studentClass = Student.class;    if (studentClass.isAnnotationPresent(IsNotInherited.class)){      System.out.println(studentClass.getAnnotation(IsNotInherited.class).value());    }  }}

没有输出任何任容,由此可知未拥有@Inherited注解的注解的类的子类不会被继承该注解。

关于“java元注解@Inherited怎么用”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

推荐阅读:
  1. java 元注解图
  2. Java annotation元注解原理的示例分析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java @inherited

上一篇:Java如何使用POI导出大数据量Excel

下一篇:如何使用Linux系统中定时运行命令

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》