java中定义enum示例

/**
 * Enumeration for the message delivery mode.  Can be persistent or
 * non persistent.  Use the method 'toInt' to get the appropriate value
 * that is used the he AMQP protocol instead of the ordinal() value when
 * passing into AMQP APIs.
 *
 * @author Mark Pollack
 * @author Gary Russell
 *
 */
public enum MessageDeliveryMode {

    NON_PERSISTENT, PERSISTENT;

    public static int toInt(MessageDeliveryMode mode) {
        switch (mode) {
        case NON_PERSISTENT:
            return 1;
        case PERSISTENT:
            return 2;
        default:
            return -1;
        }
    }

    public static MessageDeliveryMode fromInt(int modeAsNumber) {
        switch (modeAsNumber) {
        case 1:
            return NON_PERSISTENT;
        case 2:
            return PERSISTENT;
        default:
            return null;
        }
    }

}
原文地址:https://www.cnblogs.com/nizuimeiabc1/p/9619719.html