枚举类型
一.对比两种方法
利用 public static final 定义常量
public class Light {
public static final int RED=1;
public static final int GREEN=2;
public static final int YELLOW=3;
}
利用枚举类型定义常量
public enum LightA {
RED,GREEN,YELLOW
}
public enum Light {
// 利用构造函数传参
RED (1), GREEN (3), YELLOW (2);
// 定义私有变量
private int nCode ;
// 构造函数,枚举类型只能为私有
private Light( int _nCode) {
this . nCode = _nCode;
}
@Override
public String toString() {
return String.valueOf ( this . nCode );
}
}