*Java 8中的(双冒号)运算符我当时正在探索Java 8源代码,发现这个特定的代码部分非常令人惊讶://defined in IntPipeline.java@Overridepublic final OptionalInt reduce(IntBinaryOperator op) {
return evaluate(ReduceOps.makeInt(op));}@Overridepublic final OptionalInt max() {
return reduce(Math::max); //this is the gotcha line}//defined in Math.javapublic static int max(int a, int b) {
return (a >= b) ? a : b;}是Math::max类似于方法指针的东西?正常人static方法转换为IntBinaryOperator?
3 回答
慕娘9325324
TA贡献1783条经验 获得超5个赞
::
interface ConstructorReference {
T constructor();}interface MethodReference {
void anotherMethod(String input);}public class ConstructorClass {
String value;
public ConstructorClass() {
value = "default";
}
public static void method(String input) {
System.out.println(input);
}
public void nextMethod(String input) {
// operations
}
public static void main(String... args) {
// constructor reference
ConstructorReference reference = ConstructorClass::new;
ConstructorClass cc = reference.constructor();
// static method reference
MethodReference mr = cc::method;
// object method reference
MethodReference mr2 = cc::nextMethod;
System.out.println(cc.value);
}}添加回答
举报
0/150
提交
取消
