用java实现一个枚举类,有两个属性,编号和各省份地级市
时间: 2023-05-29 22:02:33 浏览: 377
public enum City {
BEIJING(1, "北京市"),
TIANJIN(2, "天津市"),
SHANGHAI(3, "上海市"),
CHONGQING(4, "重庆市"),
SHENZHEN(5, "深圳市"),
GUANGZHOU(6, "广州市"),
HANGZHOU(7, "杭州市");
private int id;
private String name;
private City(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public static City findByCode(int code) {
for (City city : values()) {
if (city.id == code) {
return city;
}
}
return null;
}
}
相关问题
用java实现一个枚举类,有两个成员属性,是编号和各省份的地级市
public enum Cities {
CITY_1(1, "北京市"), CITY_2(2, "上海市"), CITY_3(3, "天津市"), CITY_4(4, "重庆市"),
CITY_5(5, "黑龙江省"), CITY_6(6, "吉林省"), CITY_7(7, "辽宁省"), CITY_8(8, "内蒙古自治区"),
CITY_9(9, "河北省"), CITY_10(10, "山西省"), CITY_11(11, "陕西省"), CITY_12(12, "山东省"),
CITY_13(13, "新疆维吾尔自治区"), CITY_14(14, "西藏自治区"), CITY_15(15, "青海省"), CITY_16(16, "甘肃省"),
CITY_17(17, "宁夏回族自治区"), CITY_18(18, "河南省"), CITY_19(19, "江苏省"), CITY_20(20, "湖北省"),
CITY_21(21, "浙江省"), CITY_22(22, "安徽省"), CITY_23(23, "福建省"), CITY_24(24, "江西省"),
CITY_25(25, "湖南省"), CITY_26(26, "贵州省"), CITY_27(27, "四川省"), CITY_28(28, "广东省"),
CITY_29(29, "云南省"), CITY_30(30, "广西壮族自治区"), CITY_31(31, "海南省"), CITY_32(32, "台湾省"),
CITY_33(33, "香港特别行政区"), CITY_34(34, "澳门特别行政区");
private int code;
private String name;
Cities(int code, String name) {
this.code = code;
this.name = name;
}
public int getCode() {
return code;
}
public String getName() {
return name;
}
}
用java实现一个枚举类,有两个成员属性,是编号和各省份的地级市,将所有地级市都展示出来
public enum ProvinceCity {
BEIJING(1, "北京市"),
TIANJIN(2, "天津市"),
HEBEI(3, "河北省"),
SHANXI(4, "山西省"),
NEI_MONGOL(5, "内蒙古自治区"),
LIAONING(6, "辽宁省"),
JILIN(7, "吉林省"),
HEILONGJIANG(8, "黑龙江省"),
SHANGHAI(9, "上海市"),
JIANGSU(10, "江苏省"),
ZHEJIANG(11, "浙江省"),
ANHUI(12, "安徽省"),
FUJIAN(13, "福建省"),
JIANGXI(14, "江西省"),
SHANDONG(15, "山东省"),
HENAN(16, "河南省"),
HUBEI(17, "湖北省"),
HUNAN(18, "湖南省"),
GUANGDONG(19, "广东省"),
GUANGXI(20, "广西壮族自治区"),
HAINAN(21, "海南省"),
CHONGQING(22, "重庆市"),
SICHUAN(23, "四川省"),
GUIZHOU(24, "贵州省"),
YUNNAN(25, "云南省"),
XIZANG(26, "西藏自治区"),
SHANXI_II(27, "陕西省"),
GANSU(28, "甘肃省"),
QINGHAI(29, "青海省"),
NINGXIA(30, "宁夏回族自治区"),
XINJIANG(31, "新疆维吾尔自治区");
private int id;
private String cityName;
ProvinceCity(int id, String cityName) {
this.id = id;
this.cityName = cityName;
}
public int getId() {
return id;
}
public String getCityName() {
return cityName;
}
public static void main(String[] args) {
ProvinceCity[] provinceCities = ProvinceCity.values();
for (ProvinceCity pc : provinceCities) {
System.out.println(pc.getCityName());
}
}
}
阅读全文