我发现难以识别将替换return语句的运算符,因此我只需要传递运算符并忽略return语句。
我曾在C#工作,对于相同的代码,
public IWebelement usernameFiled()
{
driver.FindElement(By.id("gbqfq");
return usernameFiled;
}
也可以写成,
public IWebelement usernameFiled() => driver.FindElement(By.id("gbqfq");
我刚从Java开始,我试图找到与C#相同的运算符""""" operator(=>
)。
请有任何想法!
答案 0 :(得分:0)
您可能想要检查Java 8中的lambda表达式/方法引用和功能接口。它与您指定的不完全相同,但非常接近。你可以在没有return
语句的情况下编写lambdas(匿名函数)。简单的例子:
private WebElement waitFor(By locator, Function<By, ExpectedCondition<WebElement>> condition) {
return wait.until(condition.apply(locator));
}
第二个参数可以作为lambda表达式或方法引用传递:
waitFor(locator, by -> ExpectedConditions.elementToBeClickable(by)).click();
waitFor(locator, ExpectedConditions::elementToBeClickable).click();
elementToBeClickable
是一种返回ExpectedCondition<WebElement>
的方法,但我们不需要明确使用return
语句。