使用 for 循环:
public class Main {
public enum StudentContactRelationEnum {
MOTHER(1, "妈妈"),
FATHER(2, "爸爸"),
PATERNAL_GRANDPA(3, "爷爷"),
PATERNAL_GRANDMA(4, "奶奶"),
MATERNAL_GRANDPA(5, "外公"),
MATERNAL_GRANDMA(6, "外婆"),
OTHER(7, "其他");
private Integer code;
private String name;
private StudentContactRelationEnum(Integer code, String name) {
this.code = code;
this.name = name;
}
public Integer getCode() {
return code;
}
public String getName() {
return name;
}
}
public static void main(String[] args) {
for (StudentContactRelationEnum s : StudentContactRelationEnum.values()) {
System.out.println("----------------------------");
System.out.println(s.toString());
System.out.println(s.getCode());
System.out.println(s.getName());
}
}
}
Refs
Output:
----------------------------
MOTHER
1
妈妈
----------------------------
FATHER
2
爸爸
----------------------------
PATERNAL_GRANDPA
3
爷爷
----------------------------
PATERNAL_GRANDMA
4
奶奶
----------------------------
MATERNAL_GRANDPA
5
外公
----------------------------
MATERNAL_GRANDMA
6
外婆
----------------------------
OTHER
7
其他
使用 forEach:
import java.util.EnumSet;
import java.util.Arrays;
public class Main {
public enum StudentContactRelationEnum {
MOTHER(1, "妈妈"),
FATHER(2, "爸爸"),
PATERNAL_GRANDPA(3, "爷爷"),
PATERNAL_GRANDMA(4, "奶奶"),
MATERNAL_GRANDPA(5, "外公"),
MATERNAL_GRANDMA(6, "外婆"),
OTHER(7, "其他");
private Integer code;
private String name;
private StudentContactRelationEnum(Integer code, String name) {
this.code = code;
this.name = name;
}
public Integer getCode() {
return code;
}
public String getName() {
return name;
}
}
public static void main(String[] args) {
EnumSet.allOf(StudentContactRelationEnum.class).forEach(s -> {
System.out.println("----------------------------");
System.out.println(s.toString());
System.out.println(s.getCode());
System.out.println(s.getName());
});
/// 都是输出同样的结果
Arrays.asList(StudentContactRelationEnum.values()).forEach(s -> {
System.out.println("----------------------------");
System.out.println(s.toString());
System.out.println(s.getCode());
System.out.println(s.getName());
});
}
}
Output:
标准输出:----------------------------
MOTHER
1
妈妈
----------------------------
FATHER
2
爸爸
----------------------------
PATERNAL_GRANDPA
3
爷爷
----------------------------
PATERNAL_GRANDMA
4
奶奶
----------------------------
MATERNAL_GRANDPA
5
外公
----------------------------
MATERNAL_GRANDMA
6
外婆
----------------------------
OTHER
7
其他