java元注解@Inherited的使用详解

 更新时间:2019年11月27日 08:30:48   作者:lele  
这篇文章主要介绍了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)
@Inherited
public @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基础之StringBuffer详解

    Java基础之StringBuffer详解

    这篇文章主要介绍了Java基础之StringBuffer详解,文中有非常详细的代码示例,对正在学习java基础的小伙伴们有非常好的帮助,需要的朋友可以参考下
    2021-04-04
  • 基于XML的Spring声明事务控制

    基于XML的Spring声明事务控制

    这篇文章主要为大家详细介绍了基于XML的Spring声明事务控制,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-07-07
  • java中的类型自动转换机制解析

    java中的类型自动转换机制解析

    这篇文章主要介绍了java中的类型自动转换机制,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • java按竖线分割的实现

    java按竖线分割的实现

    在Java中,我们可以使用split()方法按竖线分割字符串,本文将介绍如何使用Java中的字符串分割方法来按竖线进行分割,同时提供代码示例来帮助读者理解,感兴趣的可以了解一下
    2024-01-01
  • java实现String字符串处理各种类型转换

    java实现String字符串处理各种类型转换

    在日常的程序开发中,经常会涉及到不同类型之间的转换,本文主要介绍了String字符串处理各种类型转换,具有一定的参考价值,感兴趣的可以了解一下
    2023-10-10
  • SpringBoot使用CommandLineRunner接口完成资源初始化方式

    SpringBoot使用CommandLineRunner接口完成资源初始化方式

    这篇文章主要介绍了SpringBoot使用CommandLineRunner接口完成资源初始化方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-02-02
  • Java统计字符串中字符出现次数的方法示例

    Java统计字符串中字符出现次数的方法示例

    这篇文章主要介绍了Java统计字符串中字符出现次数的方法,涉及Java针对字符串的遍历、查找、计算等相关操作技巧,需要的朋友可以参考下
    2017-12-12
  • Java线程的生命周期命名与获取代码实现

    Java线程的生命周期命名与获取代码实现

    这篇文章主要介绍了Java线程的生命周期命名与获取代码实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-04-04
  • Lombok基本注解之@SneakyThrows的作用

    Lombok基本注解之@SneakyThrows的作用

    @SneakyThrows注解是由lombok为咱们封装的,它能够为咱们的代码生成一个try...catch块,并把异常向上抛出来,下面这篇文章主要给大家介绍了关于Lombok基本注解之@SneakyThrows作用的相关资料,需要的朋友可以参考下
    2022-01-01
  • Java实现商品的查找、添加、出库、入库操作完整案例

    Java实现商品的查找、添加、出库、入库操作完整案例

    这篇文章主要介绍了Java实现商品的查找、添加、出库、入库操作,结合完整实例形式分析了java基于面向对象的商品信息添加、删除、查找等相关操作技巧,需要的朋友可以参考下
    2019-11-11

最新评论