枚举Enum
枚举类可以自定义多种返回类型,使用灵活、方便。
//枚举类
public enum CompareResult { //全部大写表示,并用逗号隔开 MORE,LESS,EQUALS; }
public class EnumTest { //方法完成两个数比较大小,返回大小关系 public static void main(String[] args) { CompareResult compareResult = compareNum(3, 2); //执行结果:大于 switch (compareResult){ case LESS: System.out.println("小于"); break; case MORE: System.out.println("大于"); break; case EQUALS: System.out.println("等于"); break; } } public static CompareResult compareNum(int num1,int num2){ if (num1 < num2){ return CompareResult.LESS; }else if (num1 > num2){ return CompareResult.MORE; }else { return CompareResult.EQUALS; } } }