我有以下代码段
if(!setPass){
this.teacher = Ev_TeacherTestUtil.createTeacher
(sess, 'TestTeacher1',
Ev_TeacherService.NOMINATED,
Ev_TeacherService.EMPLOYEE_NO,
Ev_TeacherService.PASS_NO);
}else{
this.speaker = Ev_TeacherTestUtil.createTeacher
(sess, 'TestTeacher1',
Ev_TeacherService.NOMINATED,
Ev_TeacherService.EMPLOYEE_NO,
Ev_TeacherService.PASS_YES);
}
else条件的唯一变化是Ev_TeacherService.PASS_YES(在if条件下设置为Ev_TeacherService.PASS_NO),其余的只是重复!我怎么能避免这个?在此先感谢您的帮助!
答案 0 :(得分:0)
这个怎么样:
this.teacher = Ev_TeacherTestUtil.createTeacher
(sess, 'TestTeacher1',
Ev_TeacherService.NOMINATED,
Ev_TeacherService.EMPLOYEE_NO,
(setPass) ? Ev_TeacherService.PASS_YES : Ev_TeacherService.PASS_NO);
答案 1 :(得分:0)
this.teacher = Ev_TeacherTestUtil.createTeacher
(sess, 'TestTeacher1',
Ev_TeacherService.NOMINATED,
Ev_TeacherService.EMPLOYEE_NO,
(!setPass)?Ev_TeacherService.PASS_YES:Ev_TeacherService.PASS_NO);
答案 2 :(得分:0)
我不确定这是哪种语言,但您通常可以使用
public void main() {
if(!setPass){
this.teacher = create(false);
} else {
this.speaker = create(true);
}
}
public Teacher create(boolean setPass) {
return Ev_TeacherTestUtil.createTeacher
(sess, 'TestTeacher1',
Ev_TeacherService.NOMINATED,
Ev_TeacherService.EMPLOYEE_NO,
setPass ? Ev_TeacherService.PASS_YES : Ev_TeacherService.PASS_NO);
}
换句话说,单行if语句应该解决这个问题。
setPass ? Ev_TeacherService.PASS_YES : Ev_TeacherService.PASS_NO
其他选项,是构建器模式