<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之旅 廣告
                <!-- Adding Methods to an enum --> ## 方法添加 除了不能繼承自一個 enum 之外,我們基本上可以將 enum 看作一個常規的類。也就是說我們可以向 enum 中添加方法。enum 甚至可以有 main() 方法。 一般來說,我們希望每個枚舉實例能夠返回對自身的描述,而不僅僅只是默認的 toString() 實現,這只能返回枚舉實例的名字。為此,你可以提供一個構造器,專門負責處理這個額外的信息,然后添加一個方法,返回這個描述信息。看一看下面的示例: ```java // enums/OzWitch.java // The witches in the land of Oz public enum OzWitch { // Instances must be defined first, before methods: WEST("Miss Gulch, aka the Wicked Witch of the West"), NORTH("Glinda, the Good Witch of the North"), EAST("Wicked Witch of the East, wearer of the Ruby " + "Slippers, crushed by Dorothy's house"), SOUTH("Good by inference, but missing"); private String description; // Constructor must be package or private access: private OzWitch(String description) { this.description = description; } public String getDescription() { return description; } public static void main(String[] args) { for(OzWitch witch : OzWitch.values()) System.out.println( witch + ": " + witch.getDescription()); } } ``` 輸出為: ``` WEST: Miss Gulch, aka the Wicked Witch of the West NORTH: Glinda, the Good Witch of the North EAST: Wicked Witch of the East, wearer of the Ruby Slippers, crushed by Dorothy's house SOUTH: Good by inference, but missing ``` 注意,如果你打算定義自己的方法,那么必須在 enum 實例序列的最后添加一個分號。同時,Java 要求你必須先定義 enum 實例。如果在定義 enum 實例之前定義了任何方法或屬性,那么在編譯時就會得到錯誤信息。 enum 中的構造器與方法和普通的類沒有區別,因為除了有少許限制之外,enum 就是一個普通的類。所以,我們可以使用 enum 做許多事情(雖然,我們一般只使用普通的枚舉類型) 在這個例子中,雖然我們有意識地將 enum 的構造器聲明為 private,但對于它的可訪問性而言,其實并沒有什么變化,因為(即使不聲明為 private)我們只能在 enum 定義的內部使用其構造器創建 enum 實例。一旦 enum 的定義結束,編譯器就不允許我們再使用其構造器來創建任何實例了。
                  <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>

                              哎呀哎呀视频在线观看