@Test
public void test1(){
Runnable r = new Runnable() {
@Override
public void run() {
System.out.print("Hello Word");
}
};
r.run();
System.out.print("===============================");
Runnable r1 = () -> System.out.print("Hello Word");
r1.run();
}
12345678910111213
2.一个参数,无返回值(x)-> System.out.print(x);
@Test
public void test2(){
Consumer<String> con = (x) -> System.out.println(x);
con.accept("Hello Word");
}
12345
如果只有一个参数,无返回值可以省略小括号不写。
3.两个参数,有返回值,并且有多条执行语句
@Test
public void test3(){
Comparator<Integer> com = (x,y) ->{
System.out.println("函数式接口");
return Integer.compare(x,y);
};
int max = com.compare(4,5);
System.out.println(max);
}
123456789
4.如果只有一条返回语句
@Test
public void test4(){
Comparator<Integer> com = (x,y) -> Integer.compare(x,y);
}
1234