import java.util.Date;
public final class Schedule {
private final Date start;
private final Date end;
public Schedule(Date start, Date end) {
if (start.compareTo(end) > 0)
throw new IllegalArgumentException(start + " after " + end);
this.start = start;
this.end = end;
}
public Date getStart() {
return start;
}
public Date getEnd() {
return end;
}
}
類中有有參構造,應該手動添加無參構造,可添加後報錯,不添加反而正常,求大神解釋下為什麼?
因為你設置兩個final的Date,又沒有初始化它們.
當你提供了一個無參的構造函數,又沒有初始化時,編譯器就會報錯.
比如改成這樣
public Schedule(){
start = new Date();
end = new Date();
}