java.util.concurrent.TimeUnit
的源代码如下:
public long convert(long sourceDuration, TimeUnit sourceUnit) {
throw new AbstractMethodError();
}
为什么这不是一个像abstract
方法一样的方法?
abstract int excessNanos(long d, long m);
java.util.concurrent.TimeUnit
的源代码如下:
public long convert(long sourceDuration, TimeUnit sourceUnit) {
throw new AbstractMethodError();
}
为什么这不是一个像abstract
方法一样的方法?
abstract int excessNanos(long d, long m);
在方法声明上方的单行注释表示以下内容:
// To maintain full signature compatibility with 1.5, and to improve the
// clarity of the generated javadoc (see 6287639: Abstract methods in
// enum classes should not be listed as abstract), method convert
// etc. are not declared abstract but otherwise act as abstract methods.
6287639
是一个 bug id,它说:
JDK-6287639 : 枚举类中的抽象方法不应列为抽象
现在考虑以下 enum
,将其视为类,每个枚举常量都是一个 Object
,很明显,如果我们创建了一个抽象的 Object
,我们必须提供实现,为了避免这种情况,convert
不是 abstract
。
enum Blocks {
A1, B1, C1;
// It will say enum constant must implement
// the abstract method test
abstract int test();
}