我班上宣布了一个很大的枚举
public enum CarMaker{
        Honda,
        Toyota,
        Sony,
        ...;
        public CarMaker at(int index){ //a method to retrieve enum by index
                CarMaker[] retval = this.values();
                return retval[index];
        }
        public final SomeObj val; //a value associated with each enum
            //.. more custom functions if needed
    }
因为每个CarMaker只需要一个实例,如果我想将这个枚举用作存储(如数组,而是使用索引访问每个元素,我可以使用更直观的名称,这是一个不好的做法.我也可以使用自定义函数对于每个元素)
CarMaker A = CarMaker.Honda;
CarMaker B = CarMaker.at(1); 
//Error above b/c 'at' is not a static member, can I make at a static member?
A.val = 5; 
B.val = 6; 
//Here I want A to be share data with B since both are "Honda"
//but it doesn't seem to do that yet
 System.out.println(A)
 System.out.println(B)
//Expected output:
//A: 6
//B: 6
现在A和B似乎创建了自己的"本田"实例,但我希望它们能够被共享.可能吗?
| 归档时间: | 
 | 
| 查看次数: | 145 次 | 
| 最近记录: |