<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 6. Enums and Annotations(枚舉和注解) ### Item 35: Use instance fields instead of ordinals(使用實例字段替代序數) Many enums are naturally associated with a single int value. All enums have an ordinal method, which returns the numerical position of each enum constant in its type. You may be tempted to derive an associated int value from the ordinal: 許多枚舉天然地與單個 int 值相關聯。所有枚舉都有一個 ordinal 方法,該方法返回枚舉類型中每個枚舉常數的數值位置。你可能想從序號中獲得一個關聯的 int 值: ``` // Abuse of ordinal to derive an associated value - DON'T DO THIS public enum Ensemble { SOLO, DUET, TRIO, QUARTET, QUINTET,SEXTET, SEPTET, OCTET, NONET, DECTET; public int numberOfMusicians() { return ordinal() + 1; } } ``` While this enum works, it is a maintenance nightmare. If the constants are reordered, the numberOfMusicians method will break. If you want to add a second enum constant associated with an int value that you’ve already used, you’re out of luck. For example, it might be nice to add a constant for double quartet, which, like an octet, consists of eight musicians, but there is no way to do it. 雖然這個枚舉可以工作,但維護卻是噩夢。如果常量被重新排序,numberOfMusicians 方法將被破壞。或者你想添加一個與已經使用過的 int 值相關聯的第二個枚舉常量,那么你就沒有那么幸運了。例如,為雙四重奏增加一個常量可能會很好,就像八重奏一樣,由八個音樂家組成,但是沒有辦法做到。 **譯注:「If you want to add a second enum constant associated with an int value that you’ve already used」是指每個常量如果不用實例字段的方式,就只能有一個序號值。實例字段可以將自定義的值對應多個常量,例如:SOLO(3), DUET(3), TRIO(3),可以都設置為序號 3** Also, you can’t add a constant for an int value without adding constants for all intervening int values. For example, suppose you want to add a constant representing a triple quartet, which consists of twelve musicians. There is no standard term for an ensemble consisting of eleven musicians, so you are forced to add a dummy constant for the unused int value (11). At best, this is ugly. If many int values are unused, it’s impractical. Luckily, there is a simple solution to these problems. **Never derive a value associated with an enum from its ordinal; store it in an instance field instead:** 此外,如果不為所有插入的 int 值添加常量,就不能為 int 值添加常量。例如,假設你想添加一個常量來表示一個由 12 位音樂家組成的三重四重奏。對于 11 位音樂家組成的合奏,由于沒有標準術語,因此你必須為未使用的 int 值(11)添加一個虛擬常量。往好的說,這僅僅是丑陋的。如果許多 int 值未使用,則不切實際。幸運的是,這些問題有一個簡單的解決方案。**不要從枚舉的序數派生與枚舉關聯的值;而是將其存儲在實例字段中:** ``` public enum Ensemble { SOLO(1), DUET(2), TRIO(3), QUARTET(4), QUINTET(5),SEXTET(6), SEPTET(7), OCTET(8), DOUBLE_QUARTET(8),NONET(9), DECTET(10),TRIPLE_QUARTET(12); private final int numberOfMusicians; Ensemble(int size) { this.numberOfMusicians = size; } public int numberOfMusicians() { return numberOfMusicians; } } ``` The Enum specification has this to say about ordinal: “Most programmers will have no use for this method. It is designed for use by general-purpose enumbased data structures such as EnumSet and EnumMap.” Unless you are writing code with this character, you are best off avoiding the ordinal method entirely. 枚舉規范對 ordinal 方法的評價是這樣的:「大多數程序員都不會去使用這個方法。它是為基于枚舉的通用數據結構(如 EnumSet 和 EnumMap)而設計的」。除非你使用這個數據結構編寫代碼,否則最好完全避免使用這個方法。 --- **[Back to contents of the chapter(返回章節目錄)](/Chapter-6/Chapter-6-Introduction.md)** - **Previous Item(上一條目):[Item 34: Use enums instead of int constants(用枚舉類型代替 int 常量)](/Chapter-6/Chapter-6-Item-34-Use-enums-instead-of-int-constants.md)** - **Next Item(下一條目):[Item 36: Use EnumSet instead of bit fields(用 EnumSet 替代位字段)](/Chapter-6/Chapter-6-Item-36-Use-EnumSet-instead-of-bit-fields.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>

                              哎呀哎呀视频在线观看