keo*_*gao 7 java enums abstract
java.util.concurrent.TimeUnit
有这个来源:
public long convert(long sourceDuration, TimeUnit sourceUnit) {
throw new AbstractMethodError();
}
Run Code Online (Sandbox Code Playgroud)
为什么这不是一个abstract
像这样的方法
abstract int excessNanos(long d, long m);
Run Code Online (Sandbox Code Playgroud)
方法声明上方的单行注释如下:
// 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.
Run Code Online (Sandbox Code Playgroud)
在这里,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();
}
Run Code Online (Sandbox Code Playgroud)