Java: Enum parameter in method

后端 未结 7 1784
小鲜肉
小鲜肉 2020-12-29 03:54

I have a method lets say:

private static String drawCellValue(
    int maxCellLength, String cellValue, String align) { }

and as you can no

7条回答
  •  粉色の甜心
    2020-12-29 04:08

    Even cooler with enums you can use switch:

    switch (align) {
       case LEFT: { 
          // do stuff
          break;
       }
       case RIGHT: {
          // do stuff
          break;
       }
       default: { //added TOP_RIGHT but forgot about it?
          throw new IllegalArgumentException("Can't yet handle " + align);
    
       }
    }
    

    Enums are cool because the output of the exception will be the name of the enum value, rather than some arbitrary int value.

提交回复
热议问题