lambda表达式快速入门
@FunctionalInterface
interface Foo
{
//public void sayHello();
public int add(int x, int y);
/*
这里default只能有一个
*/
default int mul(int x, int y)
{
return x * y;
}
public static int div(int x, int y)
{
return x/y;
}
}
/**
* @auther zzyy
*
* 1 拷贝小括号, 写死右箭头, 落地大括号
* 2 @FunctionalInterface
* 3 default
* 4 static
*/
public class LambdaExpressDemo02
{
public static void main(String[] args)
{
/*Foo foo = new Foo()
{
@Override
public void sayHello()
{
System.out.println("****hello 1205");
}
@Override
public int add(int x, int y)
{
return 0;
}
};
foo.sayHello();*/
Foo foo = (int x,int y) -> {
System.out.println("come in add method");
return x + y;
};
System.out.println(foo.add(3, 5));
System.out.println(foo.mul(3, 5));
System.out.println(Foo.div(10, 2));
}
}
本文介绍Java中Lambda表达式的使用方法,包括@FunctionalInterface注解,default和static方法的使用,通过示例展示了如何定义和使用Lambda表达式来实现函数式接口中的方法。
526

被折叠的 条评论
为什么被折叠?



