switch+ 枚举
枚举:
public enum ColorTypeEnum{ GREEN, RED, ORANGE, WHITE, BLACK }
错误示范: An enum switch case label must be the unqualified name of an enumeration constant
private void TestEnum(ColorTypeEnum colorEnum){ switch (colorEnum){ case ColorType.GREEN: // 编译时报错 break; case ColorType.RED: // 编译时报错 break; case ColorType.ORANGE: // 编译时报错 break; case ColorType.WHITE: // 编译时报错 break; case ColorType.BLACK: // 编译时报错 break; default: } }
正确使用(正确写法应该是case后面的enum项不要带ColorType)
private void TestEnum(ColorType type){ switch (type){ case GREEN: break; case RED: break; case ORANGE: break; case WHITE: break; case BLACK: break; default: } }