识别对表示为双函数的非静态 java 方法的方法引用

问题描述 投票:0回答:1

考虑一堂课,说

Class Abc {
private String extraInfo;
pubilc Abc(String extraInfo) {
this.extraInfo = extraInfo;
}

// Assume Input1, Input2 extend the same class 'Input`


public Input2 method1 (Input1 input) {
 // performs some action based on the input. 
}

public Input1 method2 (Input2 input) {

}
}

我想做的是编写一个通用方法,它将这些方法的引用作为二元函数,并希望确定函数引用是方法1还是方法2。

例如。

void testSuccess (Bifunction<Abc, I, O> func) {
val obj = new Abc("text");
func.apply(obj, decideInput(func));
}
  1. 我正在尝试编写decideInput,它接受方法ref,如果ref用于method1则返回Input1类型对象,如果ref用于method2则返回Input2。

  2. 如果Input1、Input2不扩展同一个类,那么编写decideInput也会有问题吗

    Input

我知道 func 是一个 lambda,它可能会导致识别实际引用。

java functional-programming functional-interface
1个回答
0
投票

method1 和 method2 都是 Function 类型。 不确定您为什么尝试使用 BiFunction

BiFunction 将用于接受两个参数的方法。

© www.soinside.com 2019 - 2024. All rights reserved.