为什么java.util.concurrent.TimeUnit.convert抛出AbstractMethodError而不是抽象

时间:2016-04-21 07:19:53

标签: java enums abstract

java.util.concurrent.TimeUnit有这个来源:

public long convert(long sourceDuration, TimeUnit sourceUnit) {
    throw new AbstractMethodError();
}

为什么这不是像

这样的abstract方法
abstract int excessNanos(long d, long m);

1 个答案:

答案 0 :(得分:6)

方法声明上方的单行注释说明如下,

// 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是错误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();
}