有什么方法可以在 java 中传递带有 2 个输入参数的函数?
any way to pass a function with 2 input params in java?
我如何在 java 中传递一个 Function
类对象,它获得 2 个或更多参数?
我有这个代码:
public String getSrcAfterWait(final By by) {
String currentSrc;
try {
currentSrc = tryToGetSrc(by);
} catch (StaleElementReferenceException ex)
{
currentSrc = tryToGetSrc(by);
}
return currentSrc;
}
private String tryToGetSrc(By by) {
WebElement webElement = getElementAfterWaitForDisplay2(by);
String currentSrc = null;
if (webElement != null) {
currentSrc = webElement.getAttribute("src");
}
return currentSrc;
}
private String tryToGetText(final By by) {
String currentSrc = null;
WebElement webElement = getElementAfterWaitForDisplay2(by);
if (webElement != null) {
currentSrc = webElement.getText();
}
return currentSrc;
}
public String getButtonTextAfterWait(final By by) {
String currentText;
try {
currentText = tryToGetText(by);
} catch (StaleElementReferenceException ex)
{
currentText = tryToGetText(by);
}
return currentText;
}
我想这样概括它:
public <T,V> V tryGetAttribute(final By by, Function<T,V> getFunc) {
WebElement webElement = getElementAfterWaitForDisplay2(by);
V answer = null;
if (webElement != null) {
try {
answer = getFunc.apply(webElement, getFunc);//
} catch (Exception e) {
throw new RuntimeException(e);
}
}
return answer;
}
public String getButtonTextAfterWait(final By by) {
String currentText;
Function<WebElement, String> getFunc = webElement -> webElement.getText();
try {
currentText = tryGetAttribute(by, getFunc);
} catch (StaleElementReferenceException ex) {
currentText = tryGetAttribute(by, getFunc);
}
return currentText;
}
但我看不出有什么方法可以传递具有 2 个输入参数的函数。
有没有办法抽象或者效率不高?
至少在Java8,有BiFunction
参见:
https://docs.oracle.com/javase/8/docs/api/java/util/function/BiFunction.html
如果您需要更多论据;是什么阻止您简单地创建自己的 "TripleFunction",等等?
另一种选择是定义一个新的 class,它结合了您必须传递的 two/three/... 参数。
我如何在 java 中传递一个 Function
类对象,它获得 2 个或更多参数?
我有这个代码:
public String getSrcAfterWait(final By by) {
String currentSrc;
try {
currentSrc = tryToGetSrc(by);
} catch (StaleElementReferenceException ex)
{
currentSrc = tryToGetSrc(by);
}
return currentSrc;
}
private String tryToGetSrc(By by) {
WebElement webElement = getElementAfterWaitForDisplay2(by);
String currentSrc = null;
if (webElement != null) {
currentSrc = webElement.getAttribute("src");
}
return currentSrc;
}
private String tryToGetText(final By by) {
String currentSrc = null;
WebElement webElement = getElementAfterWaitForDisplay2(by);
if (webElement != null) {
currentSrc = webElement.getText();
}
return currentSrc;
}
public String getButtonTextAfterWait(final By by) {
String currentText;
try {
currentText = tryToGetText(by);
} catch (StaleElementReferenceException ex)
{
currentText = tryToGetText(by);
}
return currentText;
}
我想这样概括它:
public <T,V> V tryGetAttribute(final By by, Function<T,V> getFunc) {
WebElement webElement = getElementAfterWaitForDisplay2(by);
V answer = null;
if (webElement != null) {
try {
answer = getFunc.apply(webElement, getFunc);//
} catch (Exception e) {
throw new RuntimeException(e);
}
}
return answer;
}
public String getButtonTextAfterWait(final By by) {
String currentText;
Function<WebElement, String> getFunc = webElement -> webElement.getText();
try {
currentText = tryGetAttribute(by, getFunc);
} catch (StaleElementReferenceException ex) {
currentText = tryGetAttribute(by, getFunc);
}
return currentText;
}
但我看不出有什么方法可以传递具有 2 个输入参数的函数。
有没有办法抽象或者效率不高?
至少在Java8,有BiFunction
参见:
https://docs.oracle.com/javase/8/docs/api/java/util/function/BiFunction.html
如果您需要更多论据;是什么阻止您简单地创建自己的 "TripleFunction",等等?
另一种选择是定义一个新的 class,它结合了您必须传递的 two/three/... 参数。