【发布时间】:2021-09-14 12:23:15
【问题描述】:
在以下用例中利用可重用性的最佳解决方案是什么。 每当用户使用以下功能单击按钮时,我都试图清除字符串。 我尝试了很多方法,但没有找到任何可靠的解决方案。
private void clearFields(int i) {
if (i == 0) {
boardId = "";
stateId = "";
cityId = "";
schoolId = "";
classId = "";
orientationId = "";
studentTypeId = "";
} else if (i == 1) {
stateId = "";
cityId = "";
schoolId = "";
classId = "";
orientationId = "";
studentTypeId = "";
} else if (i == 2) {
cityId = "";
schoolId = "";
classId = "";
orientationId = "";
studentTypeId = "";
} else if (i == 3) {
schoolId = "";
classId = "";
orientationId = "";
studentTypeId = "";
} else if (i == 4) {
classId = "";
orientationId = "";
studentTypeId = "";
} else if (i == 5) {
orientationId = "";
studentTypeId = "";
} else if (i == 6) {
studentTypeId = "";
}
}
【问题讨论】:
-
你可以做内联检查...类似:
boardId = (i == 0) ? "" : boardId; stateId = (i == 0) || (i == 1) ? "" : stateId;
标签: java kotlin software-design code-reuse