Java: Enum vs. Int

前端 未结 9 2297
野的像风
野的像风 2020-11-27 14:11

When using flags in Java, I have seen two main approaches. One uses int values and a line of if-else statements. The other is to use enums and case-switch statements.

<
9条回答
  •  感情败类
    2020-11-27 14:51

    Even though this question is old, I'd like to point out what you can't do with ints

    public interface AttributeProcessor {
        public void process(AttributeLexer attributeLexer, char c);
    }
    
    public enum ParseArrayEnd implements AttributeProcessor {
        State1{
            public void process(AttributeLexer attributeLexer, char c) {
                .....}},
        State2{
            public void process(AttributeLexer attributeLexer, char c) {
                .....}}
    }
    

    And what you can do is make a map of what value is expected as a Key, and the enum as a value,

    Map map 
    map.getOrDefault(key, ParseArrayEnd.State1).process(this, c);
    

提交回复
热议问题