用java实现一个枚举类,有两个属性,编号和各省份地级市
时间: 2023-05-29 13:02:33 浏览: 352
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 ProvinceCities {
BEIJING(1, "北京市"),
TIANJIN(2, "天津市"),
SHANGHAI(3, "上海市"),
CHONGQING(4, "重庆市");
private int code;
private String city;
ProvinceCities(int code, String city) {
this.code = code;
this.city = city;
}
public int getCode() {
return code;
}
public String getCity() {
return city;
}
}
阅读全文