java 枚舉類型

來源:互聯網
上載者:User

標籤:

枚舉的實質是什嗎?

枚舉繼承了ENUM類,枚舉類型的每一個成員都以該類型的一個執行個體,而且預設的都是聲明為public static final的 ,而且枚舉裡的每一個對象 就是當前枚舉執行個體化的對象枚舉裡面所有的方法,裡面的每個對象都可以調用

enum中的方法:

A.ordinal()用來返回枚舉值在枚舉類中的順序。

B.compareTo ()比較兩個枚舉值的順序,返回順序之差。

C.values(),靜態方法,返回包含一個全部枚舉值的數組。

D.toString(),返回枚舉常量的名稱

E.valueOf(),返回指定沒弄成的枚舉類型的枚舉常量。 

 

 先看個簡單樣本:

enum FruitEnum {    APPLE(1), ORANGE(2);    //    調用建構函式來構造枚舉項    private int value = 0;    private FruitEnum(int value) {    //    必須是private的,否則編譯錯誤        this.value = value;    }    public static FruitEnum valueOf(int value) {    //    手寫的從int到enum的轉換函式        switch (value) {        case 1:            return APPLE;        case 2:            return ORANGE;        default:            return null;        }    }    public int value() {        return this.value;    }}public static void main(String[] args) {    System.out.println(FruitEnum.APPLE.value());    //    1    System.out.println(FruitEnum.ORANGE.value());    //   2    System.out.println(FruitEnum.valueOf(1));        //  FruitEnum.APPLE    System.out.println(FruitEnum.valueOf(2));        //  FruitEnum.ORANGE}

 

下面就是網路上的各種版本的用法了:
用法一:常量

public enum Color {    RED, GREEN, BLANK, YELLOW  } 
View Code

用法二:switch

enum Signal {        GREEN, YELLOW, RED    }    public class TrafficLight {        Signal color = Signal.RED;        public void change() {            switch (color) {            case RED:                color = Signal.GREEN;                break;            case YELLOW:                color = Signal.RED;                break;            case GREEN:                color = Signal.YELLOW;                break;            }        }    }
View Code

用法三:向枚舉中添加新方法

public enum Color {        RED("紅色", 1), GREEN("綠色", 2), BLANK("白色", 3), YELLO("黃色", 4);        // 成員變數        private String name;        private int index;        // 構造方法        private Color(String name, int index) {            this.name = name;            this.index = index;        }        // 普通方法        public static String getName(int index) {            for (Color c : Color.values()) {                if (c.getIndex() == index) {                    return c.name;                }            }            return null;        }        // get set 方法        public String getName() {            return name;        }        public void setName(String name) {            this.name = name;        }        public int getIndex() {            return index;        }        public void setIndex(int index) {            this.index = index;        }    }
View Code

用法四:覆蓋枚舉的方法

public class Test {    public enum Color {        RED("紅色", 1), GREEN("綠色", 2), BLANK("白色", 3), YELLO("黃色", 4);        // 成員變數        private String name;        private int index;        // 構造方法        private Color(String name, int index) {            this.name = name;            this.index = index;        }        // 覆蓋方法        @Override        public String toString() {            return this.index + "_" + this.name;        }    }    public static void main(String[] args) {        System.out.println(Color.RED.toString());    }}
View Code

用法五:實現介面

public interface Behaviour {        void print();        String getInfo();    }    public enum Color implements Behaviour {        RED("紅色", 1), GREEN("綠色", 2), BLANK("白色", 3), YELLO("黃色", 4);        // 成員變數        private String name;        private int index;        // 構造方法        private Color(String name, int index) {            this.name = name;            this.index = index;        }        // 介面方法        @Override        public String getInfo() {            return this.name;        }        // 介面方法        @Override        public void print() {            System.out.println(this.index + ":" + this.name);        }    }
View Code

 

 

 

java 枚舉類型

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.