Calculator
接口有抽象calculate
方法和ramdom()
非抽象方法。我想在具体类中使用super ramdom()
和Override 。我的问题是为什么我必须这样打电话?为什么不工作?ramdom()
CalculatorImpl
Calculator.super.ramdom()
super.ramdon()
public interface Calculator {
double calculate(int a);
default double ramdom() {
System.out.println("Calculator ramdom");
return Math.random();
}
}
class CalculatorImpl implements Calculator {
@Override
public double calculate(int a) {
// calling super.ramdom() will get [The method ramdom() is undefined for
// the type Object error]
return Calculator.super.ramdom() * a;
}
@Override
public double ramdom() {
System.out.println("CalculatorImpl ramdom");
return 0;
}
}