<legend id='J5uD6'><style id='J5uD6'><dir id='J5uD6'><q id='J5uD6'></q></dir></style></legend>

      <i id='J5uD6'><tr id='J5uD6'><dt id='J5uD6'><q id='J5uD6'><span id='J5uD6'><b id='J5uD6'><form id='J5uD6'><ins id='J5uD6'></ins><ul id='J5uD6'></ul><sub id='J5uD6'></sub></form><legend id='J5uD6'></legend><bdo id='J5uD6'><pre id='J5uD6'><center id='J5uD6'></center></pre></bdo></b><th id='J5uD6'></th></span></q></dt></tr></i><div id='J5uD6'><tfoot id='J5uD6'></tfoot><dl id='J5uD6'><fieldset id='J5uD6'></fieldset></dl></div>

        <tfoot id='J5uD6'></tfoot>
      1. <small id='J5uD6'></small><noframes id='J5uD6'>

        • <bdo id='J5uD6'></bdo><ul id='J5uD6'></ul>

        如何在 Java 中使用 @inherited 注解?

        时间:2023-09-27

            <tbody id='lImRn'></tbody>
            <legend id='lImRn'><style id='lImRn'><dir id='lImRn'><q id='lImRn'></q></dir></style></legend>

            1. <small id='lImRn'></small><noframes id='lImRn'>

              <i id='lImRn'><tr id='lImRn'><dt id='lImRn'><q id='lImRn'><span id='lImRn'><b id='lImRn'><form id='lImRn'><ins id='lImRn'></ins><ul id='lImRn'></ul><sub id='lImRn'></sub></form><legend id='lImRn'></legend><bdo id='lImRn'><pre id='lImRn'><center id='lImRn'></center></pre></bdo></b><th id='lImRn'></th></span></q></dt></tr></i><div id='lImRn'><tfoot id='lImRn'></tfoot><dl id='lImRn'><fieldset id='lImRn'></fieldset></dl></div>
            2. <tfoot id='lImRn'></tfoot>
                <bdo id='lImRn'></bdo><ul id='lImRn'></ul>

                  本文介绍了如何在 Java 中使用 @inherited 注解?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我没有在 Java 中获得 @Inherited 注释.如果它自动为您继承方法,那么如果我需要以自己的方式实现该方法,那又如何呢?

                  I am not getting the @Inherited annotation in Java. If it automatically inherits the methods for you then if I need to implement the method in my own way then what about that ?

                  它将如何知道我的实现方式?

                  How does will it come to know my way of implementation ?

                  另外据说如果我不想使用它,而是以老式的 Java 方式来做,我必须实现 equals()toString()Object类的hashCode()方法,以及java.lang.annotation.Annotation类的注解类型方法.

                  Plus it is said if I do not want to use this and do it rather in an old fashioned Java way I have to implement the the equals(), toString(), and the hashCode() methods of the Object class and also the annotation type method of the java.lang.annotation.Annotation class.

                  这是为什么呢?

                  即使我不知道 @Inherited 注释和过去也可以正常工作的程序,我也从未实现过这些.

                  I have never implemented those even when I did not know about the @Inherited annotation and the programs used to work fine also .

                  请有人从头开始解释一下.

                  Please somebody explain me from the scratch about this.

                  推荐答案

                  只是没有误会:你确实问java.lang.annotation.Inherited.这是注解的注解.表示被注解的类的子类被认为与它们的超类具有相同的注解.

                  Just that there is no misunderstanding: You do ask about java.lang.annotation.Inherited. This is a annotation for annotations.It means that subclasses of annotated classes are considered having the same annotation as their superclass.

                  考虑以下 2 个注释:

                  Consider the following 2 Annotations:

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

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

                  如果三个类是这样注释的:

                  If three classes are annotated like this:

                  @UninheritedAnnotationType
                  class A {
                      
                  }
                  
                  @InheritedAnnotationType
                  class B extends A {
                      
                  }
                  
                  class C extends B {
                      
                  }
                  

                  运行此代码

                  System.out.println(new A().getClass().getAnnotation(InheritedAnnotationType.class));
                  System.out.println(new B().getClass().getAnnotation(InheritedAnnotationType.class));
                  System.out.println(new C().getClass().getAnnotation(InheritedAnnotationType.class));
                  System.out.println("_________________________________");
                  System.out.println(new A().getClass().getAnnotation(UninheritedAnnotationType.class));
                  System.out.println(new B().getClass().getAnnotation(UninheritedAnnotationType.class));
                  System.out.println(new C().getClass().getAnnotation(UninheritedAnnotationType.class));
                  

                  会打印出类似这样的结果(取决于注解的包):

                  will print a result similar to this (depending on the packages of the annotation):

                  null
                  @InheritedAnnotationType()
                  @InheritedAnnotationType()
                  _________________________________
                  @UninheritedAnnotationType()
                  null
                  null
                  

                  如您所见,UninheritedAnnotationType 没有被继承,但 CB 继承注释 InheritedAnnotationType.

                  As you can see UninheritedAnnotationType is not inherited but C inherits annotation InheritedAnnotationType from B.

                  我不知道这与什么方法有关.

                  这篇关于如何在 Java 中使用 @inherited 注解?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:使用弹簧定型的优点? 下一篇:java - 如何在Java中构建时使用注释动态生成代码

                  相关文章

                  最新文章

                  <legend id='7x061'><style id='7x061'><dir id='7x061'><q id='7x061'></q></dir></style></legend>
                  <tfoot id='7x061'></tfoot>
                      <bdo id='7x061'></bdo><ul id='7x061'></ul>

                  1. <i id='7x061'><tr id='7x061'><dt id='7x061'><q id='7x061'><span id='7x061'><b id='7x061'><form id='7x061'><ins id='7x061'></ins><ul id='7x061'></ul><sub id='7x061'></sub></form><legend id='7x061'></legend><bdo id='7x061'><pre id='7x061'><center id='7x061'></center></pre></bdo></b><th id='7x061'></th></span></q></dt></tr></i><div id='7x061'><tfoot id='7x061'></tfoot><dl id='7x061'><fieldset id='7x061'></fieldset></dl></div>

                      <small id='7x061'></small><noframes id='7x061'>