java 中变量名的替换
Substitutions in variable name in java
不确定这在 JAVA 中是否可行,也不知道它叫什么,所以我不确定要寻找什么,但如果可能的话,我真的很想实现相同的在我的逻辑中。我知道我们可以替换值但是我不确定如何替换变量。
public interface Constants {
GENDER_${MALE} = "where Gender = 'MALE' ";
GENDER_${FEMALE} = "where Gender = 'FEMALE' ";
}
public class Impl {
System.out.println(Constants.GENDER_MALE);
}
Expected O/P : where Gender = 'MALE';
我只是不想使用 if else 条件,if Male then return male specific where clause else female.. 因为在实际代码中几乎有 30-35 个 if else 条件,只想如果可能的话,减少它。
不可能这个词没有为我定义。
方法一
像这样制作class,
class Constants {
public String MALE = "where Gender = 'MALE' ";
public String FEMALE = "where Gender = 'FEMALE' ";
public static String GENDER_(String $argument){
Constants constants = new Constants();
try {
Field field = constants.getClass().getField($argument);
return (String) field.get($GENDER);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
return null;
}
}
}
那就这样用吧,
String $GENDER; // This is your argument, either take it from the user.
System.out.println(Constants.GENDER_($GENDER));
这将完全满足您的要求。是的,我们在这里没有使用任何逻辑 (if-else)。
注意 :如果参数不是 Constants
[=40 中的字段,Constants.GENDER_($GENDER)
将 return 为 null =]
方法二
使用 Map
而不是创建字段。像这样,
Map<String,String> fields = new HashMap<>();
fields.put("MALE","where Gender = 'MALE'");
fields.put("FEMALE","where Gender = 'FEMALE'");
然后像这样使用那个地图,
String $GENDER; // Your argument.
System.out.println(fields.get($GENDER));
public enum Gender {
FEMALE, MALE;
public String where() {
return "where Gender = '" + this + "' ";
}
}
String s = Gender.MALE.where();
因为这可能是大规模建模的尝试:
public static <T extends Enum<T>> String where(T value) {
return "where Gender = '" + value + "' ";
}
不要对这一切期望太多结果。
您可以在 Map 中放置 where 表达式,其中 key 是您的变量可能值,例如:
HashMap<String, String> whereMap = new HashMap<>();
whereMap.put("MALE", "where Gender = 'MALE' ");
whereMap.put("FEMALE", "where Gender = 'FEMALE' ");
并且不使用条件,而是通过键值检索进行替换:
String varr = "MALE";
System.out.println(MessageFormat.format("SELECT * FROM EMP {0}", whereMap.get(varr)));
不确定这在 JAVA 中是否可行,也不知道它叫什么,所以我不确定要寻找什么,但如果可能的话,我真的很想实现相同的在我的逻辑中。我知道我们可以替换值但是我不确定如何替换变量。
public interface Constants {
GENDER_${MALE} = "where Gender = 'MALE' ";
GENDER_${FEMALE} = "where Gender = 'FEMALE' ";
}
public class Impl {
System.out.println(Constants.GENDER_MALE);
}
Expected O/P : where Gender = 'MALE';
我只是不想使用 if else 条件,if Male then return male specific where clause else female.. 因为在实际代码中几乎有 30-35 个 if else 条件,只想如果可能的话,减少它。
不可能这个词没有为我定义。
方法一
像这样制作class,
class Constants {
public String MALE = "where Gender = 'MALE' ";
public String FEMALE = "where Gender = 'FEMALE' ";
public static String GENDER_(String $argument){
Constants constants = new Constants();
try {
Field field = constants.getClass().getField($argument);
return (String) field.get($GENDER);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
return null;
}
}
}
那就这样用吧,
String $GENDER; // This is your argument, either take it from the user.
System.out.println(Constants.GENDER_($GENDER));
这将完全满足您的要求。是的,我们在这里没有使用任何逻辑 (if-else)。
注意 :如果参数不是 Constants
[=40 中的字段,Constants.GENDER_($GENDER)
将 return 为 null =]
方法二
使用 Map
而不是创建字段。像这样,
Map<String,String> fields = new HashMap<>();
fields.put("MALE","where Gender = 'MALE'");
fields.put("FEMALE","where Gender = 'FEMALE'");
然后像这样使用那个地图,
String $GENDER; // Your argument.
System.out.println(fields.get($GENDER));
public enum Gender {
FEMALE, MALE;
public String where() {
return "where Gender = '" + this + "' ";
}
}
String s = Gender.MALE.where();
因为这可能是大规模建模的尝试:
public static <T extends Enum<T>> String where(T value) {
return "where Gender = '" + value + "' ";
}
不要对这一切期望太多结果。
您可以在 Map 中放置 where 表达式,其中 key 是您的变量可能值,例如:
HashMap<String, String> whereMap = new HashMap<>();
whereMap.put("MALE", "where Gender = 'MALE' ");
whereMap.put("FEMALE", "where Gender = 'FEMALE' ");
并且不使用条件,而是通过键值检索进行替换:
String varr = "MALE";
System.out.println(MessageFormat.format("SELECT * FROM EMP {0}", whereMap.get(varr)));