Java Enums can have behavior?

前端 未结 7 1397
清酒与你
清酒与你 2021-02-02 16:56

In Java, an Enum can do the great things that Enums do, but can also have methods (behavior and logic). What advantage does that have over using a class using an enum? Simple ex

7条回答
  •  逝去的感伤
    2021-02-02 17:10

    Here's a simple example:

    enum RoundingMode {
        UP {
            public double round(double d) {
                return Math.ceil(d);
            }
        },
        DOWN {
            public double round(double d) {
                return Math.floor(d);
            }
        };
    
        public abstract double round(double d);
    }
    

提交回复
热议问题