<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、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ## Chapter 4. Classes and Interfaces(類和接口) ### Item 22: Use interfaces only to define types(接口只用于定義類型) When a class implements an interface, the interface serves as a type that can be used to refer to instances of the class. That a class implements an interface should therefore say something about what a client can do with instances of the class. It is inappropriate to define an interface for any other purpose. 當一個類實現了一個接口時,這個接口作為一種類型,可以用來引用類的實例。因此,實現接口的類應該說明使用者可以對類的實例做什么。為其他任何目的定義接口都是不合適的。 One kind of interface that fails this test is the so-called constant interface. Such an interface contains no methods; it consists solely of static final fields, each exporting a constant. Classes using these constants implement the interface to avoid the need to qualify constant names with a class name. Here is an example: 不滿足上述條件的一種接口是所謂的常量接口。這樣的接口不包含任何方法;它僅由靜態 final 字段組成,每個字段導出一個常量。使用這些常量的類實現接口,以避免用類名修飾常量名。下面是一個例子: ``` // Constant interface antipattern - do not use! public interface PhysicalConstants { // Avogadro's number (1/mol) static final double AVOGADROS_NUMBER = 6.022_140_857e23; // Boltzmann constant (J/K) static final double BOLTZMANN_CONSTANT = 1.380_648_52e-23; // Mass of the electron (kg) static final double ELECTRON_MASS = 9.109_383_56e-31; } ``` The constant interface pattern is a poor use of interfaces. That a class uses some constants internally is an implementation detail. Implementing a constant interface causes this implementation detail to leak into the class’s exported API. It is of no consequence to the users of a class that the class implements a constant interface. In fact, it may even confuse them. Worse, it represents a commitment: if in a future release the class is modified so that it no longer needs to use the constants, it still must implement the interface to ensure binary compatibility. If a nonfinal class implements a constant interface, all of its subclasses will have their namespaces polluted by the constants in the interface. 常量接口模式是使用接口的糟糕方式。類內部會使用一些常量,這是實現細節。然而,實現常量接口會導致這個實現細節泄漏到類的導出 API 中。對于類的用戶來說,類實現一個常量接口沒有什么價值。事實上,這甚至會讓他們感到困惑。更糟糕的是,它代表了一種承諾:如果在將來的版本中修改了類,使其不再需要使用常量,那么它仍然必須實現接口以確保二進制兼容性。如果一個非 final 類實現了一個常量接口,那么它的所有子類的命名空間都會被接口中的常量所污染。 There are several constant interfaces in the Java platform libraries, such as java.io.ObjectStreamConstants. These interfaces should be regarded as anomalies and should not be emulated. Java 庫中有幾個常量接口,例如 `java.io.ObjectStreamConstants`。這些接口應該被視為反例,不應該被效仿。 If you want to export constants, there are several reasonable choices. If the constants are strongly tied to an existing class or interface, you should add them to the class or interface. For example, all of the boxed numerical primitive classes, such as Integer and Double, export MIN_VALUE and MAX_VALUE constants. If the constants are best viewed as members of an enumerated type, you should export them with an enum type (Item 34). Otherwise, you should export the constants with a noninstantiable utility class (Item 4). Here is a utility class version of the PhysicalConstants example shown earlier: 如果你想導出常量,有幾個合理的選擇。如果這些常量與現有的類或接口緊密綁定,則應該將它們添加到類或接口。例如,所有數值包裝類,比如 Integer 和 Double,都導出 MIN_VALUE 和 MAX_VALUE 常量。如果將這些常量看作枚舉類型的成員,那么應該使用 enum 類型導出它們([Item-34](/Chapter-6/Chapter-6-Item-34-Use-enums-instead-of-int-constants.md))。否則,你應該使用不可實例化的工具類([Item-4](/Chapter-2/Chapter-2-Item-4-Enforce-noninstantiability-with-a-private-constructor.md))導出常量。下面是一個之前的 PhysicalConstants 例子的工具類另一個版本: ``` // Constant utility class package com.effectivejava.science; public class PhysicalConstants { private PhysicalConstants() { } // Prevents instantiation(將構造私有,阻止實例化) public static final double AVOGADROS_NUMBER = 6.022_140_857e23; public static final double BOLTZMANN_CONST = 1.380_648_52e-23; public static final double ELECTRON_MASS = 9.109_383_56e-31; } ``` Incidentally, note the use of the underscore character ( _ ) in the numeric literals. Underscores, which have been legal since Java 7, have no effect on the values of numeric literals, but can make them much easier to read if used with discretion. Consider adding underscores to numeric literals, whether fixed of floating point, if they contain five or more consecutive digits. For base ten literals, whether integral or floating point, you should use underscores to separate literals into groups of three digits indicating positive and negative powers of one thousand. 順便說一下,注意可以在數字字面值中使用下劃線( _ )。下劃線自 Java 7 以來一直是合法的,它對數字字面值沒有影響,如果謹慎使用,可以使它們更容易閱讀。無論是不是固定的浮點數,如果它們包含五個或多個連續數字,都可以考慮添加下劃線到數字字面值。對于以 10 為基數的字面值,無論是整數還是浮點數,都應該使用下劃線將字面值分隔為三位數,表示 1000 的正冪和負冪。 Normally a utility class requires clients to qualify constant names with a class name, for example, PhysicalConstants.AVOGADROS_NUMBER. If you make heavy use of the constants exported by a utility class, you can avoid the need for qualifying the constants with the class name by making use of the static import facility: 通常,工具類要求客戶端使用類名來限定常量名,例如 `PhysicalConstants.AVOGADROS_NUMBER`。如果你大量使用工具類導出的常量,你可以通過使用靜態導入機制來避免使用類名限定常量: ``` // Use of static import to avoid qualifying constants import static com.effectivejava.science.PhysicalConstants.*; public class Test { double atoms(double mols) { return AVOGADROS_NUMBER * mols; } ... // Many more uses of PhysicalConstants justify static import } ``` In summary, interfaces should be used only to define types. They should not be used merely to export constants. 總之,接口應該只用于定義類型。它們不應該用于導出常量。 --- **[Back to contents of the chapter(返回章節目錄)](/Chapter-4/Chapter-4-Introduction.md)** - **Previous Item(上一條目):[Item 21: Design interfaces for posterity(為后代設計接口)](/Chapter-4/Chapter-4-Item-21-Design-interfaces-for-posterity.md)** - **Next Item(下一條目):[Item 23: Prefer class hierarchies to tagged classes(類層次結構優于帶標簽的類)](/Chapter-4/Chapter-4-Item-23-Prefer-class-hierarchies-to-tagged-classes.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>

                              哎呀哎呀视频在线观看