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

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ## Chapter 2. Creating and Destroying Objects(創建和銷毀對象) ### Item 4: Enforce noninstantiability with a private constructor(用私有構造函數實施不可實例化) Occasionally you’ll want to write a class that is just a grouping of static methods and static fields. Such classes have acquired a bad reputation because some people abuse them to avoid thinking in terms of objects, but they do have valid uses. They can be used to group related methods on primitive values or arrays, in the manner of java.lang.Math or java.util.Arrays. They can also be used to group static methods, including factories (Item 1), for objects that implement some interface, in the manner of java.util.Collections. (As of Java 8, you can also put such methods in the interface, assuming it’s yours to modify.) Lastly, such classes can be used to group methods on a final class, since you can’t put them in a subclass. 有時你會想要寫一個類,它只是一個靜態方法和靜態字段的組合。這樣的類已經獲得了壞名聲,因為有些人濫用它們來避免從對象角度思考,但是它們確有用途。它們可以用 java.lang.Math 或 java.util.Arrays 的方式,用于與原始值或數組相關的方法。它們還可以用于對以 java.util.Collections 的方式實現某些接口的對象分組靜態方法,包括工廠([Item-1](/Chapter-2/Chapter-2-Item-1-Consider-static-factory-methods-instead-of-constructors.md))。(對于 Java 8,你也可以將這些方法放入接口中,假設你可以進行修改。)最后,這些類可用于對 final 類上的方法進行分組,因為你不能將它們放在子類中。 Such utility classes were not designed to be instantiated: an instance would be nonsensical. In the absence of explicit constructors, however, the compiler provides a public, parameterless default constructor. To a user, this constructor is indistinguishable from any other. It is not uncommon to see unintentionally instantiable classes in published APIs. 這樣的實用程序類不是為實例化而設計的:實例是無意義的。然而,在沒有顯式構造函數的情況下,編譯器提供了一個公共的、無參數的默認構造函數。對于用戶來說,這個構造函數與其他構造函數沒有區別。在已發布的 API 中看到無意中實例化的類是很常見的。 **Attempting to enforce noninstantiability by making a class abstract does not work.** The class can be subclassed and the subclass instantiated. Furthermore, it misleads the user into thinking the class was designed for inheritance (Item 19). There is, however, a simple idiom to ensure noninstantiability. A default constructor is generated only if a class contains no explicit constructors, so **a class can be made noninstantiable by including a private constructor:** **譯注:原文 noninstantiable 應修改為 non-instantiable ,譯為「不可實例化的」** **試圖通過使類抽象來實施不可實例化是行不通的。** 可以對類進行子類化,并實例化子類。此外,它誤導用戶認為類是為繼承而設計的([Item-19](/Chapter-4/Chapter-4-Item-19-Design-and-document-for-inheritance-or-else-prohibit-it.md))。然而,有一個簡單的習慣用法來確保不可實例化。只有當類不包含顯式構造函數時,才會生成默認構造函數,因此**可以通過包含私有構造函數使類不可實例化:** ``` // Noninstantiable utility class public class UtilityClass { // Suppress default constructor for noninstantiability private UtilityClass() { throw new AssertionError(); } ... // Remainder omitted } ``` Because the explicit constructor is private, it is inaccessible outside the class.The AssertionError isn’t strictly required, but it provides insurance in case the constructor is accidentally invoked from within the class. It guarantees the class will never be instantiated under any circumstances. This idiom is mildly counterintuitive because the constructor is provided expressly so that it cannot be invoked. It is therefore wise to include a comment, as shown earlier. 因為顯式構造函數是私有的,所以在類之外是不可訪問的。AssertionError 不是嚴格要求的,但是它提供了保障,以防構造函數意外地被調用。它保證類在任何情況下都不會被實例化。這個習慣用法有點違反常規,因為構造函數是明確提供的,但不能調用它。因此,如上述代碼所示,包含注釋是明智的做法。 As a side effect, this idiom also prevents the class from being subclassed. All constructors must invoke a superclass constructor, explicitly or implicitly, and a subclass would have no accessible superclass constructor to invoke. 這個習慣用法也防止了類被子類化,這是一個副作用。所有子類構造函數都必須調用超類構造函數,無論是顯式的還是隱式的,但這種情況下子類都沒有可訪問的超類構造函數可調用。 --- **[Back to contents of the chapter(返回章節目錄)](/Chapter-2/Chapter-2-Introduction.md)** - **Previous Item(上一條目):[Item 3: Enforce the singleton property with a private constructor or an enum type(使用私有構造函數或枚舉類型實施單例屬性)](/Chapter-2/Chapter-2-Item-3-Enforce-the-singleton-property-with-a-private-constructor-or-an-enum-type.md)** - **Next Item(下一條目):[Item 5: Prefer dependency injection to hardwiring resources(依賴注入優于硬連接資源)](/Chapter-2/Chapter-2-Item-5-Prefer-dependency-injection-to-hardwiring-resources.md)**
                  <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>

                              哎呀哎呀视频在线观看