枚举是一种特殊的类,其中包含一组特殊的类,这些对象不会发生改变,一般都是用大写的标识符
枚举使用enum关键字
枚举类会将对象放置在最前面,那么和后面的语法要用分号隔开
public class test{
public static void main(String[] args) {
System.out.println(City.GUILING.name); //桂林
System.out.println(City.BEIJING.name); //北京
System.out.println(MyCity.GUILING.code); //10086
System.out.println(MyCity.BEIJING.code); //10001
}
}
/**
* 枚举类
*/
enum City {
BEIJING("北京",10001),GUILING("桂林",10086);
City(String name, int code) {
this.name = name;
this.code = code;
}
public String name;
public int code;
}
/**
* 普通类的实现方法
*/
class MyCity {
public String name;
public int code;
private MyCity(String name, int code) {
this.name = name;
this.code = code;
}
public static final MyCity BEIJING = new MyCity("北京",10001);
public static final MyCity GUILING = new MyCity("桂林",10086);
}