我有一个枚举类型:
public enum Operation {
PLUS() {
@Override
double apply(double x, double y) {
// ERROR: Cannot make a static reference
// to the non-static method printMe()...
printMe(x);
return x + y;
}
};
private void printMe(double val) {
System.out.println("val = " + val);
}
abstract double apply(double x, double y);
}
Run Code Online (Sandbox Code Playgroud)
如上所述,我定义了一种enum有价值的类型PLUS.它包含一个不变的特定体.在它的正文中,我试着打电话 printMe(val);,但我得到了编译错误:
无法对非静态方法printMe()进行静态引用.
为什么我会收到此错误?我的意思是我在PLUS体内覆盖了抽象方法.为什么它在static范围内?如何摆脱它?
我知道添加一个static关键字来printMe(){...}解决问题,但我有兴趣知道如果我想保持printMe()非静态是否还有其他方法?
另一个问题,与上面的问题非常类似,但这次错误消息反过来说,即PLUS(){...}具有非静态上下文:
public enum Operation {
PLUS() …Run Code Online (Sandbox Code Playgroud)