预期输出';测试通过';对于Java8示例,没有返回
我对Java8非常陌生,我正在尝试使用lambda表达式创建一个示例程序 我想在driver.getTitle()方法返回“Home Page-Safe2Pay Application”时打印“Test Passed” 我实施了两种不同的方法。方法1是正常的Java工作流,它在控制台中正确地打印输出“测试通过”。 但是使用Java8的方法2不起作用预期输出';测试通过';对于Java8示例,没有返回,java,implementation,Java,Implementation,我对Java8非常陌生,我正在尝试使用lambda表达式创建一个示例程序 我想在driver.getTitle()方法返回“Home Page-Safe2Pay Application”时打印“Test Passed” 我实施了两种不同的方法。方法1是正常的Java工作流,它在控制台中正确地打印输出“测试通过”。 但是使用Java8的方法2不起作用 String expectedTitle = "Home Page - Safe2Pay Application"; String actualTi
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");
};
创建实例后,仍然需要调用自定义函数接口的方法。由于您没有发布GetTitle类,因此我将给出一个小示例,说明如何使用另一个自定义函数接口工作
// 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.
}
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);
}
}
我希望这能让您充分了解函数接口的工作原理。您必须声明一个GetTitle接口并调用该接口内的方法
// 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.
}
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);
}
}
接口可以在类内部,也可以在类外部。方法2只是一个函数定义,但您没有执行它。
GetTitle是什么?
?当您使用lambda时,您基本上保存了一个方法供以后调用。通常会使用另一个方法调用该方法,如apply()
或run()
或其他方法,具体取决于存储该方法的类型。它可能有助于修复键入错误,以便读者可以自己运行该方法FunctionInterface
以大写字母F
开头,您在第2行拼错了运算符。谢谢,我们会解决它的。当你在移动设备上工作时的打字错误:不要责怪开发者,我完全有能力在任何键盘上做出打字错误。