设计模式混淆了java中的getTime和getTimeImpl方法。util。日期
public long getTime() {
return getTimeImpl();
}
private final long getTimeImpl() {
if (cdate != null && !cdate.isNormalized()) {
normalize();
}
return fastTime;
}
我对代码段有点困惑。 他为什么不这样写呢? 我认为它可能更可读
public long getTime(){
if (cdate != null && !cdate.isNormalized()) {
normalize();
}
return fastTime;
}
# 1 楼答案
因为
Date.writeObject()
也调用Date.getTimeImpl()
编辑:是的,
getTime()
只调用getTimeImpl()
。我认为getTimeImpl()
是将来可以更改的私有实现,而getTime()
是不能更改的公共API# 2 楼答案
我认为原因是可以扩展Date类并重写getTime()方法