在抽象类中,我有以下定义:
protected abstract A expectedA(B b);
protected Function<A, B> createExpectedA(Long foo) {
return a -> { ... return b}}
然后我想用 createExpectedA
的返回函数覆盖抽象函数,如下所示:
@Override
protected Function<A, B> expectedA = createExpectedA(fee);
但是这给了我以下错误:
The annotation @Override is disallowed for this location
如何在 Java8 中做我想做的事情?
最佳答案
注释Override
旨在用于方法而不是字段,这就是您收到此错误的原因。提醒一下,这里是 Javadoc:
Indicates that a method declaration is intended to override a method declaration in a supertype. If a method is annotated with this annotation type compilers are required to generate an error message unless at least one of the following conditions hold:
- The method does override or implement a method declared in a supertype.
- The method has a signature that is override-equivalent to that of any public method declared in Object.
你想做的事情似乎是这样的:
@Override
protected A expectedA(B b) {
return createExpectedA(fee).apply(b);
}
关于java - java中使用高阶函数重写抽象方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38896645/