枚举enum简单响应码案例
状态码枚举类代码:
package demo; public enum ERROR { SUCCESS(200,"响应成功"), ERR(500,"响应失败"); private int code; private String message; ERROR(int code,String message) { this.code = code; this.message = message; } public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
测试案例:
package demo; import java.util.Scanner; public class R{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.next(); if (str.equals("yes")) { System.out.println(ERROR.SUCCESS.getMessage()); }else if(str.equals("no")){ System.out.println(ERROR.ERR.getMessage()); } } }
运行结果:(输入yes和no)