zoukankan      html  css  js  c++  java
  • Java中的枚举enum

    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

  • 相关阅读:
    《吊打面试官》系列-缓存雪崩、击穿、穿透
    WebGL学习之纹理贴图
    小试小程序云开发
    关于socket.io的使用
    动画函数的绘制及自定义动画函数
    canvas实现俄罗斯方块
    Redis集群
    手工搭建基于ABP的框架
    手工搭建基于ABP的框架(3)
    手工搭建基于ABP的框架(2)
  • 原文地址:https://www.cnblogs.com/cnhome/p/1913498.html
Copyright © 2011-2022 走看看