我有一个问题,有点理论:
假设,我有以下课程:
interface ReportInterface {
void execute();
}
class Report implements ReportInterface {
private final Repository rep;
Report(Repository ref){
this.rep = ref;
}
public void execute(){
//do some logic
}
}
class ReportWithSetter implements ReportInterface {
private final Repository rep;
private String release;
ReportWithSetter(Repository ref){
rep = ref;
}
public void execute(){
if (release == null) throw IlligalArgumentException("release is not specified");
//do some logic
}
public void setRelease(String release){
this.release=release;
}
}
Run Code Online (Sandbox Code Playgroud)
第二个报告需要一个额外的参数释放才能正常工作,但我的接口定义为没有execute方法参数,所以我使用 setter 方法解决它,所以它看起来像:
ReportWithSetter …Run Code Online (Sandbox Code Playgroud)