我有一个类,比如Test;在那里是一个嵌套的静态类,比如TestParams。只有确定变量是否在类中声明
TestParams包含一些字符串变量指的是Test类
我现在面临的问题是,在测试类的制定者,我需要验证,如果设定的参数是声明的则params的一个Params类。
public class Test {
protected String n;
protected int num;
public static class TestParams {
public static final String PARAM_N="n";
public static final String PARAM_NUM="num";
}
public void setParam(String key, Object value) {
// Need to check here the if key is defined in TestParams
// keep adding conditions to IF statement when more params added??
if(key.equals(TestParams.PARAM_N) || (key.equals(TestParams.PARAM_NUM))
// Do some stuff
}
}
是否有任何以取代多个条件IF语句的方法:
该方案将在下面的代码所示? (类似如果TestParams()键或者一些其他的设计代码结构
你可以尝试反射 – talex 2014-10-07 11:40:29
做一个枚举并使用'Enum.valueOf(String)!= null'? – 2014-10-07 11:41:07
但是,使用所有可能的密钥并使用'contains'方法只是有'Set'这不容易吗? –
talex
2014-10-07 11:41:38