java中枚举类型,整型,字符串之间转换先说说简单的枚举类型,与字符串,整型之间的转换。
可以理解为,枚举,就是⼀个简单的数组
public enum Weather {
winter,
spring,
java数组字符串转数组summer,
fall
}
public class EnumDemo {
public static void main(String[] args) {
//  System.out.println();
/
/枚举转换整型
int winter = dinal();
System.out.println(winter);
System.out.println(dinal());
System.out.println(dinal());
System.out.println(dinal());
//枚举转换字符串。
String win = String();
String win2 = Weather.winter.name();
System.out.println(win);
System.out.println(win2);
/
/字符转换枚举
Weather weather = Weather.valueOf("spring");
System.out.println(weather);
//整型转换枚举
int winter0 = 2;
Weather weather2 = Weather.values()[winter0];
System.out.println(weather2);
}
}
带参数枚举
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.businessType = businessType;
}
public int getType() {
return type;
}
public void setType(int 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;
}
}
public enum BusinessType {
USER_BUSINESS,
ORDER_BUSINESS
}
  demo:
public class BusinessEnumDemo {
public static void main(String[] args) {
//  System.out.println();
BusinessEnum business1 = EnumByType(1);  BusinessEnum business2 = EnumByType(2);  System.out.println(business1);
System.out.println(business2);
BusinessType type1 = BusinessType();
BusinessType type2 = BusinessType();
System.out.println(type1);
System.out.println(type2);
}
}
  输出:
User
Order
USER_BUSINESS
ORDER_BUSINESS

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。