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

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Java 枚舉示例 > 原文: [https://javatutorial.net/java-enum-example](https://javatutorial.net/java-enum-example) 枚舉類型是一種特殊的數據類型,它具有不同的常量,例如`WHITE`,`BLACK`,`RED`。 約定是,它們應以大寫字母命名,因為它們又是常量。 在 Java 中,您可以使用`enum`關鍵字定義枚舉類型。 ![java-featured-image](https://img.kancloud.cn/05/3e/053ee0bb59842d92359246c98f815e0c_780x330.jpg) ```java public enum Macronutrients { FATS, CARBOHYDRATES, PROTEIN } ``` 如果您知道編譯時程序的所有可能常量,則應以枚舉類型表示它們。 Java 在 1.5 中引入了 enum 數據類型。 其他編程語言(例如 C++ 甚至 C)也具有枚舉數據類型,但是在 Java 中,它更強大。 例如,在 C/C++ 中,枚舉只是整數值的列表,而在 Java 中,它是擴展`Enum`的類本身,并且通常對讀取和寫入都更好。 最重要的是,由于枚舉是一個類的事實,它還提供了允許在枚舉成員上進行迭代的不同方法。 在 Java 中,您還可以使用`name()`方法,該方法將為您提供枚舉數據類型的成員值: ```java public class Main { public enum Macronutrients { FAT, CARBOHYDRATES, PROTEIN; } public static void main(String[] args) { Macronutrients m = Macronutrients.PROTEIN; System.out.println(m.name()); // PROTEIN } } ``` **輸出** ```java PROTEIN ``` Java 中的枚舉還為我們提供了靈活性–可以在類內部或外部聲明它們。 在類之外聲明的枚舉: ```java enum Macronutrients { FAT, CARBOHYDRATES, PROTEIN, NONE } public class Main { public static void main(String[] args) { Macronutrients m = Macronutrients.NONE; System.out.println(m.name()); // PROTEIN } } ``` **輸出** ```java NONE ``` 在類內聲明的枚舉: ```java public class Main { public enum Macronutrients { FAT, CARBOHYDRATES, PROTEIN, NONE } public static void main(String[] args) { Macronutrients m = Macronutrients.NONE; System.out.println(m.name()); // PROTEIN } } ``` **輸出** ```java NONE ``` 每個枚舉都是枚舉類型的對象。 還記得我說的枚舉賦予我們靈活性嗎? 好吧,它也可以作為參數傳遞給`switch`語句。 **使用`switch`語句**的示例 ```java enum Macronutrients { FAT, CARBOHYDRATES, PROTEIN, NONE } public class Main { Macronutrients macro; public Main(Macronutrients macro) { this.macro = macro; } public void whichMacro() { switch (macro) { case FAT: System.out.println("You've chosen FAT. A bit unhealthy, if it is the bad type of it."); break; case CARBOHYDRATES: System.out.println("You've chosen CARBOHYDRATES. Let's hope it's not sugars, right?"); break; case PROTEIN: System.out.println("You've chosen PROTEIN. Smart decision."); break; default: System.out.println("You have not chosen anything. You must be starving.."); break; } } public static void main(String[] args) { Macronutrients carbs = Macronutrients.CARBOHYDRATES; Macronutrients fats = Macronutrients.FAT; Macronutrients protein = Macronutrients.PROTEIN; Macronutrients nothing = Macronutrients.NONE; Main instance1 = new Main(carbs); instance1.whichMacro(); Main instance2 = new Main(fats); instance2.whichMacro(); Main instance3 = new Main(protein); instance3.whichMacro(); Main instance4 = new Main(nothing); instance4.whichMacro(); } } ``` **輸出** ```java You've chosen CARBOHYDRATES. Let's hope it's not sugars, right? You've chosen FAT. A bit unhealthy, if it is the bad type of it. You've chosen PROTEIN. Smart decision. You have not chosen anything. You must be starving.. ``` **要記住的關鍵事項**: * 由于枚舉是公共靜態最終,這意味著可以使用枚舉名稱來訪問它,而且由于它是最終的,因此我們無法創建該枚舉的子代。 類和枚舉之間到底有什么區別? 這是您可能會遇到的一個問題,它將是完全有效的! **類和枚舉之間的主要區別** * 枚舉擴展了`java.land.Enum`,并為它提供了人類可讀的`.toString`方法,`.name`和`.ordinal`方法等。 * 枚舉可用于`switch`語句 * 枚舉構造函數為我們提供了一些額外的支持方法,例如`values()`,`valueOf()`等,這些方法被證明確實有用。
                  <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>

                              哎呀哎呀视频在线观看