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

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # Java 枚舉字符串 > 原文: [https://www.programiz.com/java-programming/enum-string](https://www.programiz.com/java-programming/enum-string) #### 在本教程中,我們將學習了解枚舉常量的字符串值。 我們還將借助示例學習覆蓋枚舉常量的默認字符串值。 ## Java 枚舉字符串 在學習枚舉字符串之前,請確保了解 [Java 枚舉](https://www.programiz.com/java-programming/enums)。 在 Java 中,我們可以使用`toString()`方法或`name()`方法獲取枚舉常量的字符串表示形式。 例如, ```java enum Size { SMALL, MEDIUM, LARGE, EXTRALARGE } class Main { public static void main(String[] args) { System.out.println("string value of SMALL is " + Size.SMALL.toString()); System.out.println("string value of MEDIUM is " + Size.MEDIUM.name()); } } ``` **輸出** ```java string value of SMALL is SMALL string value of MEDIUM is MEDIUM ``` 在上面的示例中,我們已經看到枚舉常量的默認字符串表示形式是相同常量的名稱。 * * * ## 更改枚舉的默認字符串值 我們可以通過覆蓋`toString()`方法來更改枚舉常量的默認字符串表示形式。 例如, ```java enum Size { SMALL { // overriding toString() for SMALL public String toString() { return "The size is small."; } }, MEDIUM { // overriding toString() for MEDIUM public String toString() { return "The size is medium."; } }; } class Main { public static void main(String[] args) { System.out.println(Size.MEDIUM.toString()); } } ``` **輸出**: ```java The size is medium. ``` 在上面的程序中,我們創建了一個枚舉`size`。 而且,我們已經為枚舉常量`SMALL`和`MEDIUM`覆蓋了`toString()`方法。 **注意**:我們無法覆蓋`name()`方法。 這是因為`name()`方法是`final`。 要了解更多信息,請訪問[創建枚舉字符串](https://stackoverflow.com/questions/3978654/best-way-to-create-enum-of-strings)的最佳方法。
                  <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>

                              哎呀哎呀视频在线观看