在C中,我有时使用诸如的结构
enum {
UIViewAutoresizingNone = 0,
UIViewAutoresizingFlexibleLeftMargin = 1 << 0,
UIViewAutoresizingFlexibleWidth = 1 << 1,
UIViewAutoresizingFlexibleRightMargin = 1 << 2,
UIViewAutoresizingFlexibleTopMargin = 1 << 3,
UIViewAutoresizingFlexibleHeight = 1 << 4,
UIViewAutoresizingFlexibleBottomMargin = 1 << 5
};
typedef NSUInteger UIViewAutoresizing;
Run Code Online (Sandbox Code Playgroud)
有没有Java等价物?
hel*_*hod 15
使用EnumSet.
摘自上面的Link作为示例:
Run Code Online (Sandbox Code Playgroud)package resolver; import java.util.EnumSet; public class EnumPatternExample { public enum Style { BOLD, ITALIC, UNDERLINE, STRIKETHROUGH } public static void main(String[] args) { final EnumSet<Style> styles = EnumSet.noneOf(Style.class); styles.addAll(EnumSet.range(Style.BOLD, Style.STRIKETHROUGH)); // enable all constants styles.removeAll(EnumSet.of(Style.UNDERLINE, Style.STRIKETHROUGH)); // disable a couple assert EnumSet.of(Style.BOLD, Style.ITALIC).equals(styles); // check set contents are correct System.out.println(styles); } }
你不再需要在java中使用枚举的二进制逻辑了.你只需要enum自己和EnumSet.
例如:
enum Color {
Red, Green, Blue, Orange, White, Black
}
...
EnumSet<Color> mainColors = EnumSet.of(Color.Red, Color.Green, Color.Blue);
Color color = getSomeColor();
if (mainColors.contains(color)) {
//mainColors is like Red | Green | Blue,
//and contains() is like color & mainColors
System.out.println("Your color is either red or blue or green");
}
Run Code Online (Sandbox Code Playgroud)