java - 将非覆盖派生类方法的方法引用传递给其基类是否合法且已定义?
问题描述
将非覆盖派生类方法的方法引用传递给其基类是否合法且已定义?
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; }
}
推荐阅读
- angular - 删除嵌套表单组内的对象
- mysql - mysql查询不尊重日期参数
- java - 如何定义一个 Edit_Text 以在所有 java 代码中使用它
- python - rpy2 无法在 Mac 上加载 R 库 ggplot2
- angular - JSON 中位置 0 的意外标记 Z
- r - 在 if else 语句中向变量添加数字
- python - 通过 Python Shell 打开图像
- text - 如何从我的 Rust 中的 HTML 中删除所有空格和 ASCII 元素,如“\n”?
- javascript - 我们可以为 mongo grid fs 添加另一个值吗?
- mysql - MySQL中的Field()在hive中有什么功能吗?