<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之旅 廣告
                [TOC] # 簡介 用enum關鍵字 枚舉類,他相當于一個類繼承了Enum類而已 ~~~ public abstract class Enum<E extends Enum<E>> extends object implements Comparable<E>, Serializable ~~~ Enum是一個抽象類,里面定義的構造如下 ~~~ protected Enum(String name, int ordinal) ~~~ Enum類的構造方法依然是被封裝的,所以也屬于構造方法私有化的應用,多例設計的前提構造方法私有化 在Enum類里面定義了2個方法: * 取得枚舉的名字(public final String name()) * 取得枚舉的索引(public final int ordinal()) 還有一個values()方法,他把枚舉對象以對象數組的形式全部返回 ## 案列 ~~~ package com.study; enum Color { RED, GREEN, BLUE; } public class HelloWorld { public static void main(String[] args) { Color red = Color.RED; System.out.println(red); //RED for (Color c : Color.values()) { System.out.println(c.ordinal() + "-" + c.name()); } /* 輸出 0-RED 1-GREEN 2-BLUE */ } } ~~~ # 請解釋enum和Enum的區別? * enum是一個關鍵字,而Enum是一個抽象類 * 使用enum定義的枚舉就相當于一個類繼承了Enum這個抽象類 # 定義其他結構 ## 多例 * 枚舉之中定義的構造方法不能夠使用public聲明,如果沒有無參構造請手工調用構造傳遞參數 * 枚舉對象必須 放在首行,隨后才可以定義屬性,構造,普通方法 ### 案例 ~~~ package com.study; enum Color { RED("紅色"), GREEN("藍色"), BLUE("綠色"); private String title; //屬性 //這邊構造方法只能private修飾 private Color(String title) { this.title = title; } public String toString() { return this.title; } } public class HelloWorld { public static void main(String[] args) { for (Color c : Color.values()) { System.out.println(c); } } } ~~~ 輸出 ~~~ 紅色 藍色 綠色 ~~~ ## 可以實現接口 ~~~ package com.study; interface Message { public String getTitle(); } enum Color implements Message{ RED("紅色"), GREEN("藍色"), BLUE("綠色"); private String title; //屬性 //這邊構造方法只能private修飾 private Color(String title) { this.title = title; } public String toString() { return this.title; } @Override public String getTitle() { return this.title; } } public class HelloWorld { public static void main(String[] args) { Message msg = Color.RED; System.out.println(msg); } } ~~~ 輸出 ~~~ 紅色 ~~~ ## 在每個對象后面使用匿名內部類 ~~~ package com.study; interface Message { public String getTitle(); } enum Color implements Message { RED("紅色") { public String getTitle() { return "自己的" + this; } }, GREEN("藍色") { public String getTitle() { return "自己的" + this; } }, BLUE("綠色") { public String getTitle() { return "自己的" + this; } }; private String title; //屬性 //這邊構造方法只能private修飾 private Color(String title) { this.title = title; } public String toString() { return this.title; } } public class HelloWorld { public static void main(String[] args) { Message msg = Color.RED; System.out.println(msg.getTitle()); } } ~~~ 輸出 `自己的紅色` ## 在枚舉中定義抽象方法并覆寫 此時每個枚舉必須覆寫抽象方法 ~~~ package com.study; enum Color { RED("紅色") { @Override public String getTitle() { return "自己的" + this; } }, GREEN("藍色") { @Override public String getTitle() { return "自己的" + this; } }, BLUE("綠色") { @Override public String getTitle() { return "自己的" + this; } }; private String title; //屬性 //這邊構造方法只能private修飾 private Color(String title) { this.title = title; } public String toString() { return this.title; } //定義抽象方法 public abstract String getTitle(); } public class HelloWorld { public static void main(String[] args) { System.out.println(Color.RED.getTitle()); } } ~~~ 輸出 `自己的紅色` # 枚舉的作用 可以在switch上使用 ~~~ enum Color { RED, GREEN, BLUE; } public class HelloWorld { public static void main(String[] args) { Color c = Color.RED; //支持枚舉判斷 switch (c) { case RED: System.out.println("這是紅色"); break; case GREEN: System.out.println("這是綠色"); break; case BLUE: System.out.println("這是藍色"); break; } } } ~~~
                  <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>

                              哎呀哎呀视频在线观看