<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>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ?# 第四章第二節--處理注解 標簽(空格分隔): 廖雪峰 --- ## 注解的處理分類 - 注解本身對代碼邏輯沒有任何影響 - SOURCE類型的注解在編譯期就被丟棄了. - CLASS類型的注解僅保留在class文件中,處理的時候需要專用工具進行操作. - RUNTIME類型的注解在運行期間可以被讀取. - 如何使用注解由工具決定. 因為個人寫的代碼不涉及編譯器,所以無法處理SOURCE類型的代碼,CLASS類型的注解同時是由處理class類型的工具來使用非常少見.只有RUNTIME類型的注解可以在運行的時候使用Java代碼來讀取,所以我們討論的如何處理注解,只針對的RUNTIME類型的注解. ## 如何讀取RUNTIME類型的注解. - Annotation也是Class - 所有的Annotation繼承java.lang.annotation.Annotation - 使用反射API 方法1:判斷是否存在 使用反射API讀取Annotation: - Class.isAnnotationPresent(Class) - Field.isAnnotationPresent(Class) - Method.isAnnotationPresent(Class) - Constructor.isAnnotationPresent(Class) ![判斷是不是Annotation](http://p0b921qfc.bkt.clouddn.com/18-5-18/1429737.jpg) 方法2:直接獲取 - Class.getAnnotation(Class) - Field.getAnnotation(Class) - Method.getAnnotation(Class) - Constructor.getAnnotation(Class) ![獲取Annotation](http://p0b921qfc.bkt.clouddn.com/18-5-18/55968607.jpg) 兩種使用反射API來讀取Annotation的方式: ![反射讀取Annotation](http://p0b921qfc.bkt.clouddn.com/18-5-18/42287307.jpg) 例子: 定義一個NotNull的注解 ```java import java.lang.annotation.ElementType; import java.lang.annotation.Target; @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) //必須有 public @interface NotNull { } ``` 定義一個邊界檢測的注解 ```java import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface Range { int min() default 1; int max() default 100; } ``` 應用該注解的bean對象. ```java public class Person { @NotNull public String name; @Range(max = 20) public int age; public Person(String name, int age) { this.name = name; this.age = age; } public String toString() { return "Person(name=" + name + ", age=" + age + ")"; } } ``` 檢測類,對注解進行檢測. ```java import java.lang.reflect.Field; public class Main { public static void main(String[] args) throws Exception { Person p1 = new Person("Xiao Ming", 25); Person p2 = new Person(null, 15); checkPerson(p1); checkPerson(p2); } static void checkPerson(Person p) throws Exception { System.out.println("check " + p + "..."); Class c = Person.class; for (Field f : c.getFields()) { checkField(f, p); } } static void checkField(Field f, Person p) throws Exception { if (f.isAnnotationPresent(NotNull.class)) { Object r = f.get(p); if (r == null) { System.out.println("Error: field " + f.getName() + " is null."); } } if (f.isAnnotationPresent(Range.class)) { Range range = f.getAnnotation(Range.class); int n = (Integer) f.get(p); if (n < range.min() || n > range.max()) { System.out.println("Error: field " + f.getName() + " is out of range."); } } } } ``` ## 小節小結 - 可以在運行期通過反射讀取RUNTIME類型的注解.(不能漏寫@Rentention(RetentionPolicy.RUNTIME)) - 可以通過工具處理注解來實現相應的功能. - 對javaBean的屬性值規則進行檢查. - JUnit會自動運行@Test注解的測試方法.
                  <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>

                              哎呀哎呀视频在线观看