Lambda表达式语法:(参数) -> {} ;(多行时),(参数) -> 单行语句.
范例:多行语句
@FunctionalInterface
interface IMessage{
public void print();
}
public class TestDemo{
public static void main(String[] args){
IMessage message = () -> {
System.out.println("hello");
System.out.println("world");
System.out.println("hello world");
};
message.print();
}
}
如果现在你的表达式里只有一行进行数据的返回,那么直接使用语句即可,可以不使用return
范例:直接进行计算
@FunctionalInterface
interface IMath{
int add(int a,int b);
}
public class Test17 {
public static void main(String[] args) {
IMath math = (p1,p2) ->(p1+p2);
System.out.println(math.add(20,30));
}
}