<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之旅 廣告
                ## Chapter 4. Classes and Interfaces(類和接口) ### Item 16: In public classes, use accessor methods, not public fields(在公共類中,使用訪問器方法,而不是公共字段) Occasionally, you may be tempted to write degenerate classes that serve no purpose other than to group instance fields: 有時候,可能會編寫一些退化類,這些類除了對實例字段進行分組之外,沒有其他用途: ``` // Degenerate classes like this should not be public! class Point { public double x; public double y; } ``` Because the data fields of such classes are accessed directly, these classes do not offer the benefits of encapsulation (Item 15). You can’t change the representation without changing the API, you can’t enforce invariants, and you can’t take auxiliary action when a field is accessed. Hard-line object-oriented programmers feel that such classes are anathema and should always be replaced by classes with private fields and public accessor methods (getters) and, for mutable classes, mutators (setters): 因為這些類的數據字段是直接訪問的,所以這些類沒有提供封裝的好處([Item-15](/Chapter-4/Chapter-4-Item-15-Minimize-the-accessibility-of-classes-and-members.md))。不改變 API 就不能改變表現形式,不能實現不變量,也不能在訪問字段時采取輔助操作。堅持面向對象思維的程序員會認為這樣的類是令人厭惡的,應該被使用私有字段和公共訪問方法 getter 的類所取代,對于可變類,則是賦值方法 setter: ``` // Encapsulation of data by accessor methods and mutators class Point { private double x; private double y; public Point(double x, double y) { this.x = x; this.y = y; } public double getX() { return x; } public double getY() { return y; } public void setX(double x) { this.x = x; } public void setY(double y) { this.y = y; } } ``` Certainly, the hard-liners are correct when it comes to public classes: if a class is accessible outside its package, provide accessor methods to preserve the flexibility to change the class’s internal representation. If a public class exposes its data fields, all hope of changing its representation is lost because client code can be distributed far and wide. 當然,當涉及到公共類時,強硬派是正確的:如果類可以在包之外訪問,那么提供訪問器方法來保持更改類內部表示的靈活性。如果一個公共類公開其數據字段,那么改變其表示形式的所有希望都將落空,因為客戶端代碼可以廣泛分發。 However, if a class is package-private or is a private nested class, there is nothing inherently wrong with exposing its data fields—assuming they do an adequate job of describing the abstraction provided by the class. This approach generates less visual clutter than the accessor-method approach, both in the class definition and in the client code that uses it. While the client code is tied to the class’s internal representation, this code is confined to the package containing the class. If a change in representation becomes desirable, you can make the change without touching any code outside the package. In the case of a private nested class, the scope of the change is further restricted to the enclosing class. 但是,如果一個類是包級私有的或者是私有嵌套類,那么公開它的數據字段并沒有什么本質上的錯誤(假設它們能夠很好地描述類提供的抽象)。無論是在類定義還是在使用它的客戶端代碼中,這種方法產生的視覺混亂都比訪問方法少。雖然客戶端代碼與類的內部表示綁定在一起,但這段代碼僅限于包含該類的包。如果想要對表示形式進行更改,你可以在不接觸包外部任何代碼的情況下進行更改。對于私有嵌套類,更改的范圍進一步限制在封閉類中。 Several classes in the Java platform libraries violate the advice that public classes should not expose fields directly. Prominent examples include the Point and Dimension classes in the java.awt package. Rather than examples to be emulated, these classes should be regarded as cautionary tales.As described in Item 67, the decision to expose the internals of the Dimension class resulted in a serious performance problem that is still with us today. Java 庫中的幾個類違反了公共類不應該直接公開字段的建議。突出的例子包括 `java.awt` 包中的 Point 和 Dimension。這些類不應被效仿,而應被視為警示。正如 [Item-67](/Chapter-9/Chapter-9-Item-67-Optimize-judiciously.md) 所述,公開 Dimension 類的內部結構導致了嚴重的性能問題,這種問題至今仍存在。 While it’s never a good idea for a public class to expose fields directly, it is less harmful if the fields are immutable. You can’t change the representation of such a class without changing its API, and you can’t take auxiliary actions when a field is read, but you can enforce invariants. For example, this class guarantees that each instance represents a valid time: 雖然公共類直接公開字段從來都不是一個好主意,但是如果字段是不可變的,那么危害就會小一些。你不能在不更改該類的 API 的情況下更改該類的表現形式,也不能在讀取字段時采取輔助操作,但是你可以實施不變量。例如,這個類保證每個實例代表一個有效的時間: ``` // Public class with exposed immutable fields - questionable public final class Time { private static final int HOURS_PER_DAY = 24; private static final int MINUTES_PER_HOUR = 60; public final int hour; public final int minute; public Time(int hour, int minute) { if (hour < 0 || hour >= HOURS_PER_DAY) throw new IllegalArgumentException("Hour: " + hour); if (minute < 0 || minute >= MINUTES_PER_HOUR) throw new IllegalArgumentException("Min: " + minute); this.hour = hour; this.minute = minute; } ... // Remainder omitted } ``` In summary, public classes should never expose mutable fields. It is less harmful, though still questionable, for public classes to expose immutable fields.It is, however, sometimes desirable for package-private or private nested classes to expose fields, whether mutable or immutable. 總之,公共類不應該公開可變字段。對于公共類來說,公開不可變字段的危害要小一些,但仍然存在潛在的問題。然而,有時候包級私有或私有嵌套類需要公開字段,無論這個類是可變的還是不可變的。 --- **[Back to contents of the chapter(返回章節目錄)](/Chapter-4/Chapter-4-Introduction.md)** - **Previous Item(上一條目):[Item 15: Minimize the accessibility of classes and members(盡量減少類和成員的可訪問性)](/Chapter-4/Chapter-4-Item-15-Minimize-the-accessibility-of-classes-and-members.md)** - **Next Item(下一條目):[Item 17: Minimize mutability(減少可變性)](/Chapter-4/Chapter-4-Item-17-Minimize-mutability.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>

                              哎呀哎呀视频在线观看