返回类型封装

1、普通类型

public class Result {
    public final int code;
    public final String msg;

    public Result(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }
    public Result() {
        code = 0;
        msg = "请求成功";
    }
    public static final Result SUCCESS = new Result();
    public static final Result FAILURE = new Result(-1, "操作异常");
    public static final Result PARAM_ERROR = new Result(-1, "参数错误");
}

2、带数据返回类型

public class ResultDetail extends Result{

    public final T data;

    public ResultDetail(int code, String msg, T data) {
        super(code, msg);
        this.data = data;
    }
    public ResultDetail(T data) {
        super(0, "请求成功");
        this.data = data;
    }
}

你可能感兴趣的:(返回类型封装)