s-day07--函数式接口的定义与使用
This commit is contained in:
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package com.inmind.functional_interface02;
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface MyInterface {
|
||||||
|
int getSum(int a,int b);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user