用Java写一个枚举类,有两个属性:编号和所有省市的地级市,通过编号得到地级市
时间: 2023-05-29 07:02:23 浏览: 473
public enum CityEnum {
// 枚举值
CITY_ONE(1, "北京市", "天津市", "河北省"),
CITY_TWO(2, "上海市", "江苏省", "浙江省", "安徽省"),
CITY_THREE(3, "重庆市", "四川省", "贵州省", "云南省");
// 成员变量
private int number;
private String[] cityList;
// 构造函数
private CityEnum(int number, String... cityList) {
this.number = number;
this.cityList = cityList;
}
// 根据编号获取城市列表
public static String[] getCityList(int number) {
for (CityEnum cityEnum : CityEnum.values()) {
if (cityEnum.number == number) {
return cityEnum.cityList;
}
}
return null;
}
}
阅读全文