在java中的枚举元素内部到达变量

时间:2014-12-25 08:21:03

标签: java enums

我希望枚举的每个元素都有不同的变量,但我无法达到它们。

public class Employee {
public GENERAL[] general = GENERAL.values();

public static void main(String[] args) {
    Employee e = new Employee(); 
    e.general[GENERAL.INCOME.ordinal()].salary = 10; //this line doesn't compile

}

enum GENERAL{
    INCOME{
        public int salary;
        public int tips;
    },SATIFACTION{
        //some variables
    },EFFICIENCY{
        //some variables
    };
}

}

我尝试过投射到(GENERAL.INCOME),但它没有用。有办法吗?如果这是不可能的,最好的解决方法是什么?提前谢谢。

4 个答案:

答案 0 :(得分:2)

尝试在枚举级别而不是单个元素中定义变量:

  public static void main(String[] args) {
  MainClass e = new MainClass(); 
      e.general[GENERAL.INCOME.ordinal()].salary = 10; //this line doesn't compile
      System.out.println(e.general[GENERAL.INCOME.ordinal()].salary);
  }

  enum GENERAL{
     INCOME(0,0), SATIFACTION(0, 0), EFFICIENCY(0,0);
     int salary;
     int tips;
     GENERAL(int salary, int tips){
         this.salary = salary;
         this.tips = tips;
     }
  }

答案 1 :(得分:1)

这是因为INCOME是GENERAL的匿名子类,它是这样的

static class GENERAL {
    public static GENERAL INCOME = new GENERAL() {
        public int salary;
        public int tips;
    };
}

无法访问Java中的匿名类的字段(反射除外)

答案 2 :(得分:0)

这是我能做到的最干净的方式。我还有一个可以用来迭代的数组。将军的每个元素都有自己的变量。每个元素都有一个序号用作索引号。

这种方法存在的问题是无法使用GENERAL.values()。如果稍后添加新元素,则必须手动并以正确的顺序将其添加到getList()方法。在向代码中添加新元素时很容易出错。

public class Employee {
public Object general[] = General.getList();

public static void main(String[] args) {
    Employee e = new Employee();

    General.Income i = (General.Income) e.general[General.Income.ordinal];
    i.salary = 10; //eclipse doesn't let me to combine these 2 lines into 1 expressions. 

    System.out.println(i.salary);

    // following lines demonstrates that the salary of the e.general[General.Income.ordinal] is changed. Not just the i.
    General.Income t = (General.Income) e.general[General.Income.ordinal];
    System.out.println(t.salary);

}

public static class General {
    public static Object[] getList() {
        Object general[] = { new Income(), new Satisfaction(), new Efficiency() };
        return general;
    }

    public static class Income {
        public static final int ordinal = 0;
        public int salary;
        public int tips;
    }

    public static class Satisfaction {
        public static final int ordinal() {return 1;}//using method instead of int saves memory. (8 bytes I think. Neglettable).
        // some variables
    }

    public static class Efficiency {
        public static final int ordinal = 2;
        // some variables
    }

}
}

答案 3 :(得分:0)

如果每个枚举都包含一个值,为什么不使用它?

您甚至可以添加一种方法来检索一些描述性名称:

enum General {
    INCOME, SATIFACTION, EFFICIENCY;
    int value = 0;

    String getName() {

        switch(this) {
            case INCOME:
                return "salary";

            case SATIFACTION:
                return "etc";
        }
    }
}

可以通过General.values()[i].valueGeneral.INCOME.value设置/获取这些内容,也可以添加setValue(int value)getValue()方法并制作value private