JAVA中的操作员参考

时间:2017-08-21 16:26:34

标签: java c# selenium

我发现难以识别将替换return语句的运算符,因此我只需要传递运算符并忽略return语句。

我曾在C#工作,对于相同的代码,

public IWebelement usernameFiled() 
{
    driver.FindElement(By.id("gbqfq");
    return usernameFiled; 
}

也可以写成,

 public IWebelement usernameFiled() => driver.FindElement(By.id("gbqfq");

我刚从Java开始,我试图找到与C#相同的运算符""""" operator(=>)。

请有任何想法!

1 个答案:

答案 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语句。