我有以下枚举:
enum Days{
TODAY{
@Override
public Date getLowerBound(){
another(); //1
currentUpperBound(); //2
return null;
}
@Override
public Date another() {
return null;
}
};
public abstract Date getLowerBound();
public abstract Date another();
private Date currentUpperBound(){
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
为什么//2导致编译时错误
Cannot make a static reference to the non-static method
currentUpperBound() from the type Days
Run Code Online (Sandbox Code Playgroud)
但//1编译好吗?两种方法都是非静态的.我看不出任何问题......也许它与Eclipse有关?
更新:正如@Florian Schaetz在评论中注意到的,如果我们声明方法有static private修饰符,它将正常工作.为什么?
我建议currentUpperBounds() 保护而不是私人.另一种解决方案是为您的呼叫添加前缀super.,这也有效:
@Override
public Date getLowerBound(){
another();
super.currentUpperBound();
return null;
}
Run Code Online (Sandbox Code Playgroud)
或者,今天也有效:
@Override
public Date getLowerBound(){
another();
TODAY.currentUpperBound();
return null;
}
Run Code Online (Sandbox Code Playgroud)
Mick Mnemonic在这个副本中提到了很好的答案,这很好地解释了它.