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

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ## Java 8 Annotation 新特性初體驗 ### 特性一:Type Annotation 在 Java 8 之前的版本中,只能允許在聲明式前使用 Annotation。而在 Java 8 版本中,Annotation 可以被用在任何使用 Type 的地方,例如:初始化對象時 \(new\),對象類型轉化時,使用 implements 表達式時,或者使用 throws 表達式時。 ##### 清單 1. Type Annotation 使用示例 ``` //初始化對象時 String myString = new @NotNull String(); //對象類型轉化時 myString = (@NonNull String) str; //使用 implements 表達式時 class MyList<T> implements @ReadOnly List<@ReadOnly T>{ ... } //使用 throws 表達式時 public void validateValues() throws @Critical ValidationFailedException{ ... } ``` 定義一個 Type Annotation 的方法與普通的 Annotation 類似,只需要指定 Target 為 ElementType.TYPE\_PARAMETER 或者 ElementType.TYPE\_USE,或者同時指定這兩個 Target。 ##### 清單 2. 定義 Type Annotation 示例 ``` @Target({ElementType.TYPE_PARAMETER, ElementType.TYPE_USE}) public @interface MyAnnotation { } ``` ElementType.TYPE\_PARAMETER 表示這個 Annotation 可以用在 Type 的聲明式前,而 ElementType.TYPE\_USE 表示這個 Annotation 可以用在所有使用 Type 的地方(如:泛型,類型轉換等) 與 Java 8 之前的 Annotation 類似的是,Type Annotation 也可以通過設置 Retention 在編譯后保留在 class 文件中(RetentionPolicy.CLASS)或者運行時可訪問(RetentionPolicy.RUNTIME)。但是與之前不同的是,Type Annotation 有兩個新的特性:在本地變量上的 Annotation 可以保留在 class 文件中,以及泛型類型可以被保留甚至在運行時被訪問。 雖然 Type Annotation 可以保留在 class 文件中,但是它并不會改變程序代碼本身的行為。例如在一個方法前加上 Annotation,調用此方法返回的結果和不加 Annotation 的時候一致。 Java 8 通過引入 Type Annotation,使得開發者可以在更多的地方使用 Annotation,從而能夠更全面地對代碼進行分析以及進行更強的類型檢查。 ### 特性二:Repeating Annotation 在實際應用中,可能會出現需要對同一個聲明式或者類型加上相同的 Annotation(包含不同的屬性值)的情況。 例如系統中除了管理員之外,還添加了超級管理員這一權限,對于某些只能由這兩種角色調用的特定方法,可以使用 Repeating Annotation。 ##### 清單 3. Repeating Annotation 使用示例-1 ``` @Access(role="SuperAdministrator") @Access(role="Administrator") public void doCheck() { ...... } ``` 上面的示例是針對方法使用 Annotation, 開發者也可以根據產品中的具體需求在其他地方使用 Repeating Annotation。例如某個類專門提供管理員相關的功能,可以直接在這個類上標注同樣的 Annotation。 ##### 清單 4. Repeating Annotation 使用示例-2 ``` @Access(role="SuperAdministrator") @Access(role="Administrator") public class AdminServices{ } ``` 之前版本的 JDK 并不允許開發者在同一個聲明式前加注同樣的 Annotation,(即使屬性值不同)這樣的代碼在編譯過程中會提示錯誤。而 Java 8 解除了這一限制,開發者可以根據各自系統中的實際需求在所有可以使用 Annotation 的地方使用 Repeating Annotation。 由于兼容性的緣故,Repeating Annotation 并不是所有新定義的 Annotation 的默認特性,需要開發者根據自己的需求決定新定義的 Annotation 是否可以重復標注。Java 編譯器會自動把 Repeating Annotation 儲存到指定的 Container Annotation 中。而為了觸發編譯器進行這一操作,開發者需要進行以下的定義: 首先,在需要重復標注特性的 Annotation 前加上 @Repeatable 標簽,示例如下: ##### 清單 5. 定義 Repeating Annotation 示例 ``` @Repeatable(AccessContainer.class) public @interface Access { String role(); } ``` @Repeatable 標簽后括號中的值即為指定的 Container Annotation 的類型。在這個例子中,Container Annotation 的類型是 AccessContainer,Java 編譯器會把重復的 Access 對象保存在 AccessContainer 中。 AccessContainer 中必須定義返回數組類型的 value 方法。數組中元素的類型必須為對應的 Repeating Annotation 類型。具體示例如下: ##### 清單 6. 定義 Container Annotation 示例 ``` public @interface AccessContainer { Access[] value(); } ``` 可以通過 Java 的反射機制獲取注解的 Annotation。一種方式是通過 AnnotatedElement 接口的 getAnnotationByType\(Class&lt;T&gt;\) 首先獲得 Container Annotation,然后再通過 Container Annotation 的 value 方法獲得 Repeating Annotation。另一種方式是用過 AnnotatedElement 接口的 getAnnotations\(Class&lt;T&gt;\) 方法一次性返回 Repeating Annotation。 Repeating Annotation 使得開發者可以根據具體的需求對同一個聲明式或者類型加上同一類型的注解,從而增加代碼的靈活性和可讀性。
                  <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>

                              哎呀哎呀视频在线观看