将对非重写派生类方法的方法引用传递给其基类是否合法且已定义?
public class Base {
private Supplier<Int> intSupplier;
public Base(Supplier<Int> intSupplier) {
this.intSupplier = intSupplier;
}
public Int getInt() {
return inSupplier.get();
}
}
。
public class Derived extends Base {
public Derived() {
super(this::returnsOne);
}
private Int returnsOne() { return 1; }
}
.
assert(1 == new Derived().getInt())
最佳答案
为什么不正常做呢?
public class Derived extends Base {
@Override
public Int getInt() {
returnsOne();
}
private Int returnsOne() { return 1; }
}
关于java - 将非重写派生类方法的方法引用传递给其基类是否合法且已定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58228635/