gpt4 book ai didi

java - 将枚举描述与变量参数相结合

转载 作者:行者123 更新时间:2023-12-01 21:14:19 26 4
gpt4 key购买 nike

我有 ENUM 类,我用它来构建类似这样的响应对象。

    if(statusCode == 0){
userModel.setResponseCode(BOFAStatusCodes.ACTION_TAKEN.getErrorCode());
userModel.setResponseMessage(BOFAStatusCodes.ACTION_TAKEN.getDescription());
}

这是枚举。

public enum BOFAStatusCodes {
ACTION_TAKEN(0,"An action has been taken that successfully completes the request."),
NO_ACTION(800,"No action was necessary to complete the request successfully."),
AUTH_FAILED(403,"Authentication failed."),
FAILURE_CLIENT(905,"The request cannot be completed due to an error with the client's request."),
FAILURE_SERVICE(500,"The request cannot be completed due to an error with the service's processing of the request.");

private String description;
private int errorCode;

private BOFAStatusCodes(int errorCode, String description){
this.errorCode=errorCode;
this.description = description;
}
public int getErrorCode() {
return errorCode;
}

public String getDescription() {
return description;
}

}

但现在的要求是他们需要 userID 以及消息,例如:

An action has been taken that successfully completes the request for the User XXX.

任何建议。

最佳答案

您可以在枚举中使用字符串格式

ACTION_TAKEN(0,"An action has been taken that successfully completes the request for the user %s.")

在你的类(class):

if(statusCode == 0){
userModel.setResponseCode(BOFAStatusCodes.ACTION_TAKEN.getErrorCode());
userModel.setResponseMessage(String.format(BOFAStatusCodes.ACTION_TAKEN.getDescription(), username));
}

关于java - 将枚举描述与变量参数相结合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40594864/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com