用java实现一个枚举类,通过编号得到40位当代明星
时间: 2023-05-27 17:05:11 浏览: 115
public enum StarEnum {
ONE("0000000000000000000000000000000000000001", "周杰伦"),
TWO("0000000000000000000000000000000000000002", "林俊杰"),
THREE("0000000000000000000000000000000000000003", "蔡依林"),
FOUR("0000000000000000000000000000000000000004", "张靓颖"),
FIVE("0000000000000000000000000000000000000005", "邓紫棋");
private final String id;
private final String name;
StarEnum(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public static String getNameById(String id) {
for (StarEnum star : StarEnum.values()) {
if (star.getId().equals(id)) {
return star.getName();
}
}
return null;
}
}
//测试代码
public class Test {
public static void main(String[] args) {
String id = "0000000000000000000000000000000000000002";
String name = StarEnum.getNameById(id);
System.out.println(name);
}
}
阅读全文