s-day07--函数式接口的定义与使用

This commit is contained in:
xx
2026-10-20 11:17:43 +08:00
parent 532d2b2b4e
commit e3ccc87545
2 changed files with 29 additions and 0 deletions
@@ -0,0 +1,23 @@
package com.inmind.functional_interface02;
//函数式接口:有且仅有一个必须重写的抽象方法的接口
public class Demo03 {
public static void main(String[] args) {
int i = 10;
int j = 20;
//调用使用函数式接口的方法
/*int result = getResult(new MyInterface() {
@Override
public int getSum(int a, int b) {
return a+b;
}
}, i, j);*/
// int result = getResult((int a, int b)->{return a+b;}, i, j);
int result = getResult(( a, b)-> a+b, i, j);
System.out.println(result);
}
public static int getResult(MyInterface myInterface,int a,int b) {
int sum = myInterface.getSum(a, b);
return sum;
}
}