我需要一种在java中构造常量树的方法。
对于API,我有:操作/请求名称,每个操作都有特定数量的参数
API_V00
action_1: param_1,param_2 ...;
action_2: param_3,param_4 ...;
......
我想要action_1
,action_2
,param_1
,param_2
等,表示为常量/枚举,并将其作为:
API_V00.action_1 -> this should return a string with value "action_1"
API_V00.action_2 -> this should return a string with value "action_2"
API_V00.action_1.param_1 -> this should return a string with value "param_1"
API_V00.action_1.param_2 -> this should return a string with value "param_2"
API_V00.action_2.param_3 -> this should return a string with value "param_3"
API_V00.action_2.param_4 ......
我尝试使用操作创建枚举,我有API_V00.action_1
- >返回一个字符串,但现在我没有办法将prams名称绑定到每个动作,以便将这种访问类型赋予params action_1.param_1
等。
答案 0 :(得分:0)
听起来你应该创建一个简单的类来包装action + param对。如果你想让它更像常量,你可以将构造函数设为私有,并提供一些预先实例化的#34;实例。例如,这样的事情可能有用:
public class ParameterizedAction {
public static ParameterizedAction API_V00
= new ParameterizedAction(action_1, "param_1", "param_2", ...);
// etc for the others
private final Action action;
private final String[] params;
private ParameterizedAction(Action action, String... parameters) {
this.action = action;
this.parameters = parameters;
}
public Action getAction() {
return action;
}
public String parameter(int idx) {
return parameters[idx];
}
}