Java枚举类型和整形的转换
简单的很,做个记录,如下枚举类,
package com.lyx.model;
/**
* Created by liyanxin on 2014/12/24.
*/
public enum Gender {
MAIL("男性"), FMAIL("女性");
private String value;
private Gender(String value) {
this.value = value;
}
@Override
public String toString() {
return this.value;
}
}
如何 把枚举值 转换成整型 int,如下,
@Test
public void test98767() {
System.out.println(Gender.MAIL.ordinal());
System.out.println(Gender.FMAIL.ordinal());
}
如何把整型转换为 枚举,如下,
@Test
public void test987() {
System.out.println(Gender.values()[0]);
System.out.println(Gender.values()[1]);
}
如何把 枚举的字符串 转化为 枚举值,如下,
@Test
public void tes987() {
System.out.println(Gender.valueOf("MAIL").name());
System.out.println(Gender.valueOf("MAIL").ordinal());
System.out.println(Enum.valueOf(Gender.class, "MAIL").name());
System.out.println(Enum.valueOf(Gender.class, "MAIL").ordinal());
}
=================================END=================================