public enum JacketSize{ small(36), medium(40),large(42),
                      extra_large(46),extra_extra_large(48);
JacketSize(int chestSize){
this.chestSize = chestSize;
}
public int chestSize(){
return chestSize;
}
private int chestSize;
}
主要问题有2个
第一:
上面的构造函数,他构造的是什么??如果是构造一个枚举的常量,那为什么是int型的行参??如果是常量后面小括号里的数字,那我觉得也不对啊,它方法主体里的代码应该不是的啊??第二:
对于这类枚举的使用套路是不是:
1、把这个Jacket类不同类型的数据分别生成不同的枚举,比如大小值、颜色值、款式值等
2、通过生成一个Jacket类,生成构造函数,将枚举中各个不同类型的值作为行参。
3、通过生成另一个类,将实参传递给构造函数,以此来生成一个有大小、颜色、款式的真正意义上的夹克对象。
是不是一定是这样的流程??而无法在枚举这个类中就直接生成这么一个对象??

解决方案 »

  1.   


    public enum Color { BLACK(0, 0, 0),
    RED(255, 0, 0),
    GREEN(0, 255, 0),
    BLUE(0, 0, 255),
    WHITE(255, 255, 255); private int r = 0;
    private int g = 0;
    private int b = 0; private Color(int r, int g, int b) {
    this.r = r;
    this.g = g;
    this.b = b;
    }}//相当于public class Color /*extends Enum*/ {  //extends Enum 这儿只是相当于,实际写出来编译会失败 public static final Color BLACK = new Color(0, 0, 0);
    public static final Color RED = new Color(255, 0, 0);
    public static final Color GREEN = new Color(0, 255, 0);
    public static final Color BLUE = new Color(0, 0, 255);
    public static final Color WHITE = new Color(255, 255, 255); private int r = 0;
    private int g = 0;
    private int b = 0; private Color(int r, int g, int b) {
    this.r = r;
    this.g = g;
    this.b = b;
    }}
      

  2.   

    话说...JAVA的ENUM真的是...非常非常...垃圾....
    还不如直接用public final static 来定义....