日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

[转载] Java对返回值的封装

發(fā)布時(shí)間:2025/3/11 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [转载] Java对返回值的封装 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

參考鏈接: 用Java封裝

定義自己所需要的返回值類型?

public class CodeMsg implements Cloneable {

? ? private int retCode;

? ? private String message;

?

? ? // 通用異常

? ? public static CodeMsg SUCCESS = new CodeMsg(0, "success");

? ? public static CodeMsg EMPTY_PARAM_ERROR = new CodeMsg(400, "參數(shù)為空");

? ? public static CodeMsg INTER_ERROR = new CodeMsg(505, "服務(wù)端異常");

?

? ? private CodeMsg(int retCode, String message) {

? ? ? ? this.retCode = retCode;

? ? ? ? this.message = message;

? ? }

?

? ? public int getRetCode() {

? ? ? ? return retCode;

? ? }

?

? ? public String getMessage() {

? ? ? ? return message;

? ? }

?

? ? public void setMessage(String message) {

? ? ? ? this.message = message;

? ? }

?

? ? @Override

? ? protected Object clone() throws CloneNotSupportedException {

? ? ? ? return (CodeMsg) super.clone();

? ? }

}?

返回值的封裝?

public class Result<T> {

? ? private String message;

? ? private int retCode;

? ? private T data;

?

? ? private Result(T data) {

? ? ? ? this.retCode = 200;

? ? ? ? this.message = "成功";

? ? ? ? this.data = data;

? ? }

?

? ? private Result(CodeMsg cm) {

? ? ? ? if (cm == null) {

? ? ? ? ? ? return;

? ? ? ? }

? ? ? ? this.retCode = cm.getRetCode();

? ? ? ? this.message = cm.getMessage();

? ? }

?

? ? /**

? ? ?* 成功時(shí)候的調(diào)用

? ? ?*

? ? ?* @return

? ? ?*/

? ? public static <T> Result<T> success(T data) {

? ? ? ? return new Result<T>(data);

? ? }

?

? ? /**

? ? ?* 成功,不需要傳入?yún)?shù)

? ? ?*

? ? ?* @return

? ? ?*/

? ? @SuppressWarnings("unchecked")

? ? public static <T> Result<T> success() {

? ? ? ? return (Result<T>) success("");

? ? }

?

? ? /**

? ? ?* 失敗時(shí)候的調(diào)用

? ? ?*

? ? ?* @return

? ? ?*/

? ? public static <T> Result<T> error(CodeMsg cm) {

? ? ? ? return new Result<T>(cm);

? ? }

?

? ? /**

? ? ?* 失敗時(shí)候的調(diào)用,擴(kuò)展消息參數(shù)

? ? ?*

? ? ?* @param cm

? ? ?* @param msg

? ? ?* @return

? ? ?*/

? ? public static <T> Result<T> error(CodeMsg cm, String msg) {

? ? ? ? CodeMsg newCodeMsg = null;

? ? ? ? try {

? ? ? ? ? ? newCodeMsg = (CodeMsg) cm.clone();

? ? ? ? } catch (Exception e) {

? ? ? ? ? ? e.printStackTrace();

? ? ? ? }

? ? ? ? newCodeMsg.setMessage(cm.getMessage() + "--" + msg);

? ? ? ? return new Result<T>(newCodeMsg);

? ? }

?

? ? public T getData() {

? ? ? ? return data;

? ? }

?

? ? public String getMessage() {

? ? ? ? return message;

? ? }

?

? ? public int getRetCode() {

? ? ? ? return retCode;

? ? }

?

?

}?

參考返回碼定義?

// 成功狀態(tài)碼

public static final int SUCCESS = 1;

?

// -------------------失敗狀態(tài)碼----------------------

// 參數(shù)錯(cuò)誤

public static final int PARAMS_IS_NULL = 10001;// 參數(shù)為空

public static final int PARAMS_NOT_COMPLETE = 10002; // 參數(shù)不全

public static final int PARAMS_TYPE_ERROR = 1003; // 參數(shù)類型錯(cuò)誤

public static final int PARAMS_IS_INVALID = 10004; // 參數(shù)無(wú)效

?

// 用戶錯(cuò)誤

public static final int USER_NOT_EXIST = 20001; // 用戶不存在

public static final int USER_NOT_LOGGED_IN = 20002; // 用戶未登陸

public static final int USER_ACCOUNT_ERROR = 20003; // 用戶名或密碼錯(cuò)誤

public static final int USER_ACCOUNT_FORBIDDEN = 20004; // 用戶賬戶已被禁用

public static final int USER_HAS_EXIST = 20005;// 用戶已存在

?

// 業(yè)務(wù)錯(cuò)誤

public static final int BUSINESS_ERROR = 30001;// 系統(tǒng)業(yè)務(wù)出現(xiàn)問(wèn)題

?

// 系統(tǒng)錯(cuò)誤

public static final int SYSTEM_INNER_ERROR = 40001; // 系統(tǒng)內(nèi)部錯(cuò)誤

?

// 數(shù)據(jù)錯(cuò)誤

public static final int DATA_NOT_FOUND = 50001; // 數(shù)據(jù)未找到

public static final int DATA_IS_WRONG = 50002;// 數(shù)據(jù)有誤

public static final int DATA_ALREADY_EXISTED = 50003;// 數(shù)據(jù)已存在

?

// 接口錯(cuò)誤

public static final int INTERFACE_INNER_INVOKE_ERROR = 60001; // 系統(tǒng)內(nèi)部接口調(diào)用異常

public static final int INTERFACE_OUTER_INVOKE_ERROR = 60002;// 系統(tǒng)外部接口調(diào)用異常

public static final int INTERFACE_FORBIDDEN = 60003;// 接口禁止訪問(wèn)

public static final int INTERFACE_ADDRESS_INVALID = 60004;// 接口地址無(wú)效

public static final int INTERFACE_REQUEST_TIMEOUT = 60005;// 接口請(qǐng)求超時(shí)

public static final int INTERFACE_EXCEED_LOAD = 60006;// 接口負(fù)載過(guò)高

?

// 權(quán)限錯(cuò)誤

public static final int PERMISSION_NO_ACCESS = 70001;// 沒(méi)有訪問(wèn)權(quán)限

總結(jié)

以上是生活随笔為你收集整理的[转载] Java对返回值的封装的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。