Java中的枚舉enum

來源:互聯網
上載者:User
public class EnumTest {    enum RadioState {        RADIO_OFF,         /* Radio explictly powered off (eg CFUN=0) */        RADIO_UNAVAILABLE, /* Radio unavailable (eg, resetting or not booted) */        SIM_NOT_READY,     /* Radio is on, but the SIM interface is not ready */        SIM_LOCKED_OR_ABSENT,  /* SIM PIN locked, PUK required, network                               personalization, or SIM absent */        SIM_READY,         /* Radio is on and SIM interface is available */        RUIM_NOT_READY,    /* Radio is on, but the RUIM interface is not ready */        RUIM_READY,        /* Radio is on and the RUIM interface is available */        RUIM_LOCKED_OR_ABSENT, /* RUIM PIN locked, PUK required, network                                  personalization locked, or RUIM absent */        NV_NOT_READY,      /* Radio is on, but the NV interface is not available */        NV_READY;          /* Radio is on and the NV interface is available */        public boolean isOn() /* and available...*/ {            return this == SIM_NOT_READY                    || this == SIM_LOCKED_OR_ABSENT                    || this == SIM_READY                    || this == RUIM_NOT_READY                    || this == RUIM_READY                    || this == RUIM_LOCKED_OR_ABSENT                    || this == NV_NOT_READY                    || this == NV_READY;        }        public boolean isAvailable() {            return this != RADIO_UNAVAILABLE;        }        public boolean isSIMReady() {            return this == SIM_READY;        }        public boolean isRUIMReady() {            return this == RUIM_READY;        }        public boolean isNVReady() {            return this == NV_READY;        }        public boolean isGsm() {            return this == SIM_NOT_READY                    || this == SIM_LOCKED_OR_ABSENT                    || this == SIM_READY;        }        public boolean isCdma() {            return this ==  RUIM_NOT_READY                    || this == RUIM_READY                    || this == RUIM_LOCKED_OR_ABSENT                    || this == NV_NOT_READY                    || this == NV_READY;        }    }public static void main(String[] args) {RadioState mRS = RadioState.RADIO_OFF;System.out.println("RadioState isOn: " + mRS.isOn());System.out.println("RadioState.RADIO_OFF: " + mRS.RADIO_OFF);}}

1. RadioState枚舉類就是class,而且是一個不可以被繼承的final類。其枚舉值(RADIO_OFF...)都是RadioState類型的靜態常量,

2. 即然枚舉類是class,當然在枚舉類型中有構造器,方法和資料域。但是,枚舉類的構造器有很大的不同: 

  (1) 構造器只是在構造枚舉值的時候被調用。  

  (2) 構造器只能私人private,絕對不允許有public構造器。

3. 所有枚舉類都繼承了Enum的方法.

 

參考文章:http://www.javaeye.com/topic/477731

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.