簡體   English   中英

JAVA中的操作員參考

[英]Operator reference in JAVA

我發現很難確定將替換return語句的運算符,因此我只需要傳遞運算符本身而忽略return語句。

我曾在C#工作,對於相同的代碼,

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

也可以寫成

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

我只是從Java開始,正在嘗試找到與C#的“跟隨”運算符( => )等效的運算符。

任何想法,請!

您可能要檢查Java 8中的lambda表達式/方法引用和功能接口。它與您指定的不完全相同,但是非常接近。 您可以編寫lambdas(匿名函數)而無需return語句。 簡單的例子:

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語句。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM