enum与int、String之间的转换

 参考:https://www.cnblogs.com/sunxucool/archive/2012/12/03/2800021.html

一、简单枚举

示例:

// 当枚举类只有一个属性时,这个枚举类是单例的
public enum Weather {
    winter,
    spring,
    summer,
    fall
}

1. 枚举转化成int

int i = Weather.winter.ordinal(); // => 0

2.int转化成枚举

Weather b= Weather.values()[0];

3.枚举转化成String

 String winter = Weather.winter.name(); // => winter

4. String转化成枚举

Weather weather = Weather.valueOf("spring");

二、带参数枚举

示例:

public enum BusinessType {
    USER_BUSINESS,
    ORDER_BUSINESS
}

public enum BusinessEnum {
    User(1, BusinessType.USER_BUSINESS),
    Order(2, BusinessType.ORDER_BUSINESS);
 
    private int type;
    private BusinessType businessType;
 
    private BusinessEnum(int type, BusinessType businessType) {
        this.type = type;
        this.businessType = businessType;
    }
 
    public int getType() {
        return type;
    }
 
    public void setType(int type) {
        this.type = type;
    }
 
    public BusinessType getBusinessType() {
        return businessType;
    }
 
    public void setBusinessType(BusinessType businessType) {
        this.businessType = businessType;
    }
 
    public static BusinessEnum getEnumByType(int type) {
        for (BusinessEnum bt : values()) {
            if (bt.type == type) {
                return bt;
            }
        }
        return null;
    }
 
}

1. int 转化成枚举

BusinessEnum business1 = BusinessEnum.getEnumByType(1); // => User

BusinessType type1 = business1.getBusinessType(); // => USER_BUSINESS

示例:

	public enum Lamtern {
		RED(200), YELLOW(30), GREEN(200);

		int time;

		private Lamtern(int time) {
			this.time = time;
		}

		public int getTime() {
			return time;
		}

		public void setTime(int time) {
			this.time = time;
		}

		public static Lamtern getLamtern(int time) {
			for (Lamtern c : Lamtern.values()) {
				if (c.getTime() == time) {
					return c;
				}
			}
			return null;
		}

	}

1.int转化成枚举

Lamtern lamtern = Lamtern.getLamtern(200);

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,一个枚举是由一组常量所组成的。这些常量在编译时就已经确定,因此与普通的变量不同,枚举的值不能随意改变。 有时候我们需要将一个枚举的值转换成整数或字符串类,或者将一个整数或字符串类的值转换枚举。这时,我们可以使用Java中提供的一些方法来实现。 如果我们要将一个枚举的值转换为整数类,可以使用枚举中的ordinal()方法。该方法返回该枚举常量在枚举中的序号,从0开始计数。示例代码如下: ``` enum Color{ RED, GREEN, BLUE } Color color = Color.RED; int index = color.ordinal(); // 0 ``` 如果要将一个整数类的值转换枚举,可以使用枚举中的values()方法。该方法返回一个包含所有枚举常量的数组。我们可以利用这个数组和整数类的值来获取要转换枚举。示例代码如下: ``` enum Color{ RED, GREEN, BLUE } int index = 1; Color color = Color.values()[index]; // GREEN ``` 如果要将一个枚举的值转换为字符串类,可以使用枚举中的name()方法。该方法返回该枚举常量的名称。示例代码如下: ``` enum Color{ RED, GREEN, BLUE } Color color = Color.RED; String name = color.name(); // "RED" ``` 如果要将一个字符串类的值转换枚举,可以使用枚举中的valueOf()方法。该方法接受一个字符串类的参数,并返回相应的枚举常量。需要注意的是,该方法对于不存在的枚举常量或空字符串会抛出IllegalArgumentException异常。示例代码如下: ``` enum Color{ RED, GREEN, BLUE } String name = "GREEN"; Color color = Color.valueOf(name); // GREEN ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值