<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                Inherited作用是,使用此注解聲明出來的自定義注解,在使用此自定義注解時,如果注解在類上面時,子類會自動繼承此注解,否則的話,子類不會繼承此注解。這里一定要記住,使用Inherited聲明出來的注解,只有在類上使用時才會有效,對方法,屬性等其他無效。 ~~~ /** * 聲明的此注解使用了Inherited元注解,表示此注解用在類上時,會被子類所繼承 * @author crazy */ @Retention(RetentionPolicy.RUNTIME) @Inherited public @interface InheritedTest { String value(); } ~~~ ~~~ /** * 聲明的此注解沒有使用Inherited元注解,表示此注解用在類上時,不會被子類所繼承 * @author crazy */ @Retention(RetentionPolicy.RUNTIME) public @interface InheritedTest2 { String value(); } ~~~ ~~~ /** * 父類 * @author crazy */ @InheritedTest("使用Inherited的注解 class") @InheritedTest2("未使用Inherited的注解 class") public class Parent { @InheritedTest("使用Inherited的注解 method") @InheritedTest2("未使用Inherited的注解 method") public void method(){ } @InheritedTest("使用Inherited的注解 method2") @InheritedTest2("未使用Inherited的注解 method2") public void method2(){ } @InheritedTest("使用Inherited的注解 field") @InheritedTest2("未使用Inherited的注解 field") public String a; } ~~~ ~~~ /** * 子類 只繼承了一個method方法 * @author crazy */ public class Child extends Parent { @Override public void method() { } } ~~~ ~~~ /** * 通過反射進行測試 * @author crazy */ public class test { public static void main(String[] args) throws NoSuchMethodException, SecurityException, NoSuchFieldException { Class<Child> clazz = Child.class; //對類進行測試 System.out.println("對類進行測試"); if(clazz.isAnnotationPresent(InheritedTest.class)){ System.out.println(clazz.getAnnotation(InheritedTest.class).value()); } if(clazz.isAnnotationPresent(InheritedTest2.class)){ System.out.println(clazz.getAnnotation(InheritedTest2.class).value()); } System.out.println(); //對方法 進行測試 System.out.println("對方法進行測試"); Method method = clazz.getMethod("method", null); if(method.isAnnotationPresent(InheritedTest.class)){ System.out.println(method.getAnnotation(InheritedTest.class).value()); } if(method.isAnnotationPresent(InheritedTest2.class)){ System.out.println(method.getAnnotation(InheritedTest2.class).value()); } System.out.println(); //對方法2 進行測試 System.out.println("對方法2進行測試"); Method method2 = clazz.getMethod("method2", null); if(method2.isAnnotationPresent(InheritedTest.class)){ System.out.println(method2.getAnnotation(InheritedTest.class).value()); } if(method2.isAnnotationPresent(InheritedTest2.class)){ System.out.println(method2.getAnnotation(InheritedTest2.class).value()); } System.out.println(); //對屬性測試 System.out.println("對屬性進行測試"); Field field = clazz.getField("a"); if(field.isAnnotationPresent(InheritedTest.class)){ System.out.println(field.getAnnotation(InheritedTest.class).value()); } if(field.isAnnotationPresent(InheritedTest2.class)){ System.out.println(field.getAnnotation(InheritedTest2.class).value()); } } } ~~~ 下面是輸出結果 ~~~ 對類進行測試 使用Inherited的注解 class 對方法進行測試 對方法2進行測試 使用Inherited的注解 method2 未使用Inherited的注解 method2 對屬性進行測試 使用Inherited的注解 field 未使用Inherited的注解 field ~~~ 由上可以看出,通過Inherited元注解聲明的自定義注解,在類上使用時,可以被子類繼承,對第一個方法進行測試時,由于子類繼承了父類方法,且兩個都沒有輸出,證明Inherited對方法無效,由方法2可以看出,因為子類沒有重寫父類方法,所以是直接使用的父類方法,所以兩個都會輸出,同理屬性也是,都會輸出。 所以證明最后結論:通過對注解上使用元注解Inherited聲明出的注解,在使用時用在類上,可以被子類所繼承,對屬性或方法無效。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看