Java 8 样本未返回预期输出 'Test Passed'
The expected output 'Test Passed' is not returned for the Java 8 Sample
我是 Java 8 的新手,我正在尝试使用 lambda 表达式创建示例程序
我想在 driver.getTitle() 方法 returns "Home Page - Safe2Pay Application".
时打印 'Test Passed'
我实施了两种不同的方法。方法 1 是正常的 Java 工作流程,它在控制台中正确打印输出 'Test Passed'。
但是方法 2,使用 Java 8 不起作用。
String expectedTitle = "Home Page - Safe2Pay Application";
String actualTitle = "";
//Approach 1
actualTitle = driver.getTitle();
if (actualTitle.contentEquals(expectedTitle)) {
System.out.println("Test Passed");
} else {
System.out.println("Test Failed");
}
//Approach 2
//Java 8 execution
GetTitle m = () -> {
if (driver.getTitle().contentEquals(expectedTitle))
System.out.println("Test Passed");
else
System.out.println("Test Failed");
};
创建实例后,您仍然需要调用自定义功能接口的方法。既然你没有 post 你的 GetTitle class,我将举一个小例子说明它应该如何使用另一个自定义功能接口工作。
// the functional interface
@FunctionalInterface
public static interface Operator {
public void operate();
}
public static void main(String[] args)
{
Operator o = () -> System.out.println("test"); //here you create a class instance of Operator.
o.operate(); // this is how you call that method/functional interface.
// this is a non-lambda example which works exactually the same, but may make things a bit more clear.
//create new instance
Operator o1 = new Operator() {
@Override
public void operate()
{
System.out.println("test");
}
};
o1.operate(); //call the method.
}
我希望这能让您对函数式接口的工作原理有足够的了解。
您必须声明一个 GetTitle 接口并调用该接口内的方法。
public class Driver {
static String expectedTitle = "Home Page - Safe2Pay Application";
static String actualTitle = "";
public static void main(String args[]){
Driver driver = new Driver();
//Approach 1
actualTitle = getTitle();
if (actualTitle.contentEquals(expectedTitle)) {
System.out.println("Test Passed");
} else {
System.out.println("Test Failed");
}
//Approach 2
//Java 8 execution
GetTitle m = (Driver dr) -> {
if (Driver.getTitle().contentEquals(expectedTitle))
System.out.println("Test Passed");
else
System.out.println("Test Failed");
};
m.operation(driver);
}
public static String getTitle(){
return expectedTitle;
}
interface GetTitle {
void operation(Driver driver);
}
}
接口可以在 class 内部或 class 外部。
我是 Java 8 的新手,我正在尝试使用 lambda 表达式创建示例程序
我想在 driver.getTitle() 方法 returns "Home Page - Safe2Pay Application".
时打印 'Test Passed'我实施了两种不同的方法。方法 1 是正常的 Java 工作流程,它在控制台中正确打印输出 'Test Passed'。 但是方法 2,使用 Java 8 不起作用。
String expectedTitle = "Home Page - Safe2Pay Application";
String actualTitle = "";
//Approach 1
actualTitle = driver.getTitle();
if (actualTitle.contentEquals(expectedTitle)) {
System.out.println("Test Passed");
} else {
System.out.println("Test Failed");
}
//Approach 2
//Java 8 execution
GetTitle m = () -> {
if (driver.getTitle().contentEquals(expectedTitle))
System.out.println("Test Passed");
else
System.out.println("Test Failed");
};
创建实例后,您仍然需要调用自定义功能接口的方法。既然你没有 post 你的 GetTitle class,我将举一个小例子说明它应该如何使用另一个自定义功能接口工作。
// the functional interface
@FunctionalInterface
public static interface Operator {
public void operate();
}
public static void main(String[] args)
{
Operator o = () -> System.out.println("test"); //here you create a class instance of Operator.
o.operate(); // this is how you call that method/functional interface.
// this is a non-lambda example which works exactually the same, but may make things a bit more clear.
//create new instance
Operator o1 = new Operator() {
@Override
public void operate()
{
System.out.println("test");
}
};
o1.operate(); //call the method.
}
我希望这能让您对函数式接口的工作原理有足够的了解。
您必须声明一个 GetTitle 接口并调用该接口内的方法。
public class Driver {
static String expectedTitle = "Home Page - Safe2Pay Application";
static String actualTitle = "";
public static void main(String args[]){
Driver driver = new Driver();
//Approach 1
actualTitle = getTitle();
if (actualTitle.contentEquals(expectedTitle)) {
System.out.println("Test Passed");
} else {
System.out.println("Test Failed");
}
//Approach 2
//Java 8 execution
GetTitle m = (Driver dr) -> {
if (Driver.getTitle().contentEquals(expectedTitle))
System.out.println("Test Passed");
else
System.out.println("Test Failed");
};
m.operation(driver);
}
public static String getTitle(){
return expectedTitle;
}
interface GetTitle {
void operation(Driver driver);
}
}
接口可以在 class 内部或 class 外部。