The cause of

In a real business, we might encounter situations where enumerations are needed to determine what business logic should be executed

While I strongly recommend using if else or state modes, sometimes they are not a quick and perfect substitute for switch

The solution

Suppose we have the following enumeration:

public private enum Person {
    MEN, WOMEN, UNKOWN
}
Copy the code

When we use enumerations in switch, we simply use “MEN”, as follows:

public class Test{
    public static void main(Person person) {
        switch (person) {
          caseMEN: Business code...break;
          caseWOMEN: Business code...break; }}}Copy the code