我研究了TimeUnit
和toMillis()
方法。
public long toMillis(long paramLong)
{
throw new AbstractMethodError();
}
toMillis()
方法除了抛出AbstractMethodError
异常外什么也不做。那么,
toMillis()
方法如何将秒,分钟等转换为毫秒? 最佳答案
打开TimeUnit并查看其中的常量...
/**
* Time unit representing one thousandth of a microsecond
*/
NANOSECONDS {
public long toNanos(long d) { return d; }
public long toMicros(long d) { return d/(C1/C0); }
public long toMillis(long d) { return d/(C2/C0); }
public long toSeconds(long d) { return d/(C3/C0); }
public long toMinutes(long d) { return d/(C4/C0); }
public long toHours(long d) { return d/(C5/C0); }
public long toDays(long d) { return d/(C6/C0); }
public long convert(long d, TimeUnit u) { return u.toNanos(d); }
int excessNanos(long d, long m) { return (int)(d - (m*C2)); }
},
/**
* Time unit representing one thousandth of a millisecond
*/
MICROSECONDS {
public long toNanos(long d) { return x(d, C1/C0, MAX/(C1/C0)); }
public long toMicros(long d) { return d; }
public long toMillis(long d) { return d/(C2/C1); }
public long toSeconds(long d) { return d/(C3/C1); }
public long toMinutes(long d) { return d/(C4/C1); }
public long toHours(long d) { return d/(C5/C1); }
public long toDays(long d) { return d/(C6/C1); }
public long convert(long d, TimeUnit u) { return u.toMicros(d); }
int excessNanos(long d, long m) { return (int)((d*C1) - (m*C2)); }
},
如您所见,枚举器TimeUnit中的每个常量
NANOSECONDS,MICROSECONDS 等实现了匿名方法,这些方法使您可以间接访问
toMillis()
,因此此方法 public long toMillis(long duration) {
throw new AbstractMethodError();
}
永远不会被您的代码直接访问...