I have the following enum in a Java class:
public enum Resolution {
RES_32 (32),
RES_64 (64);
private final int asInt;
private Resolution(int asInt) {
this.asInt = asInt;
}
};
I have more classes that need a similar kind of enum, with the same asInt property and the same constructor, but with different constants. So, in another class, I need the following enum:
public enum Resolution {
RES_32 (32),
RES_64 (64),
RES_128 (128);
private final int asInt;
private Resolution(int asInt) {
this.asInt = asInt;
}
};
If this was a class, I could use inheritance to not repeat the code in the constructor (and would likely have made a getter for that asInt property). What can I do in order to stop repeating myself each time I need such a Resolution enum? Ideally, I would like to just specify the constants for each Resolution, and to have the constructor and property kept.
EnumSetmay be helpful in this context. Given the following,Suitably named sets may be used as shown below.