所以我正在重构一些非常旧的代码,我看到相同的代码重复了大约 20 次,唯一的区别是设置为 true 或 false 的对象正在改变。我试图弄清楚如何使用 .notation 将对象设置为 true 或 false。它看起来像这样。if("true".equals(this.element.getText())) { endorse.setFDSC(true);}else { endorse.setFDSC(false);}我目前的方法是这样的。private void setEndorsmentRecordsToF( EndorsementRecordApplication endorse, String location) throws Exception { if("true".equals(this.element.getText())) { endorse.setFDSC(true); }else { endorse.setFDSC(false); }}任何提示或建议将不胜感激,这些对象中的每一个都不同,但适用相同的逻辑。
1 回答
慕村225694
TA贡献1880条经验 获得超4个赞
您可以使用Boolean.class. 所以你可以使用Boolean.parseBoolean方法。然后你的代码会是这样的;
private void setEndorsmentRecordsToF(EndorsementRecordApplication endorse, String location) throws Exception {
endorse.setFDSC(Boolean.parseBoolean(this.element.getText()));
}
在Boolean.class这个方法中,主体是;
public static boolean parseBoolean(String var0) {
return var0 != null && var0.equalsIgnoreCase("true");
}
所以检查equalsIgnoreCase。
如果要将布尔值转换为 String,请使用已定义的此方法Boolean.class;
public static String toString(boolean var0) {
return var0 ? "true" : "false";
}
添加回答
举报
0/150
提交
取消