Well, you can nearly do that:
public enum CharEnum // Let's avoid java.lang.* clashes
{
A(1), B(2), C(4), D(8);
private final int mask;
private CharEnum(int mask)
{
this.mask = mask;
}
public int getMask()
{
return mask;
}
}
Then:
CharEnum ch = /* from user */
if ((ch.getMask() & CharEnum.A.getMask()) > 0)
{
// some operation...
}
This can be useful in some situations, but as Michael said you should definitely look at EnumSet
for general "set of values" options.
If you do decide to go for an enum with values, you can put extra logic within the enum itself:
public enum CharEnum
{
// Other code as before
public boolean overlapsWith(CharEnum ch)
{
return (ch.mask & mask) > 0;
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…