上面我回复的代码是用枚举实现的单例模式,下面我再用单例模式实现枚举,代码如下:
public enum SingletonOne {
RED(255, 0, 0), GREEN(0, 255, 0), BLUE(0, 0, 255);
int redValue;
int greenValue;
int blueValue;
private SingletonOne(int red, int green, int blue) {
this.redValue = red;
this.greenValue = green;
this.blueValue = blue;
}
public int getRedValue() {
return redValue;
}
public void setRedValue(int red) {
this.redValue = red;
}
public int getGreenValue() {
return greenValue;
}
public void setGreenValue(int green) {
this.greenValue = green;
}
public int getBlueValue() {
return blueValue;
}
public void setBlueValue(int blue) {
this.blueValue = blue;
}
public static void main(String[] args) {
System.out.println(SingletonOne.RED.getRedValue());
}
}
用单例实现枚举就是把枚举中的每一个元素都写成一个单例 |