这是常用方法
//常用方法实现
public class LambdaDemo_1 {
public static void main(String[] args) {
MathOperation addition = new Addition();
addition.operation(1,2);
}
}
//定义一个函数式接口
interface MathOperation{
void operation(int a,int b);
}
//实现类
class Addition implements MathOperation{
@Override
public void operation(int a, int b) {
System.out.println(a+b);
}
}
局部内部类的实现 同一个文件包不能存在重复类
public class LambdaDemo_3 {
public static void main(String[] args) {
//实现局部内部类
class Addition implements MathOperation_3{
@Override
public void operation(int a, int b) {
System.out.println(a+b);
}
}
//执行计算
Addition addition = new Addition();
addition.operation(4,5);
}
}
interface MathOperation_3{
void operation(int a,int b);
}
匿名内部类
public class LambdaDemo_4 {
//匿名内部类,实现接口,直接执行计算
public static void main(String[] args) {
new MathOperation_4() {
@Override
public void operation(int a, int b) {
System.out.println(a+b);
}
}.operation(5,3);
}
}
interface MathOperation_4{
void operation(int a,int b);
}
public class LambdaDemo_5 {
public static void main(String[] args) {
//实现匿名内部类,实现接口
MathOperation_5 addition = new MathOperation_5() {
@Override
public void operation(int a, int b) {
System.out.println(a+b);
}
};
//执行计算
addition.operation(6,7);
}
}
interface MathOperation_5{
void operation(int a,int b);
}
初次具现Lambda
public class LambdaDemo_6 {
public static void main(String[] args) {
//Lambda简化,只有一块语句,省略接口和方法,只留下语句实现
MathOperation_6 addition = (int a, int b) -> {
System.out.println(a + b);
};
addition.operation(22,11);
}
}
interface MathOperation_6{
void operation(int a,int b);
}
Lambda表达式最终简化
public class LambdaDemo_7 {
//最终lambda简化版
public static void main(String[] args) {
MathOperation_7 addition_7 = (a,b,c) -> System.out.println(a+b+c+c);
addition_7.operation(1,2,2);
}
}
interface MathOperation_7{
void operation(int a,int b,int c);
}