文件:IFunctionQuote.java
package quote;
@FunctionalInterface
public interface IFunctionQuote<T1, T2, T3, R> {
R doSomeThing(T1 t1, T2 t2, T3 t3);
}
文件:TestQuote.java
package quote;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class TestQuote {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("list第一个元素,");
list.add("list第二个元素,");
list.add("list第三个元素,");
Set<Integer> set = new HashSet<>();
set.add(0);
set.add(1);
set.add(2);
String str = "测试字符串结尾。";
//静态函数引用
System.out.println(test(list, set, str, TestQuote::staticFun));
//实例函数引用
System.out.println(test(list, set, str, new TestQuote()::instanceFun));
System.out.println("ok");
}
public static String test(List<String> list, Set<Integer> set, String str, IFunctionQuote<List<String>, Set<Integer>, String, String> fun){
return fun.doSomeThing(list, set, str);
}
public static String staticFun(List<String> list, Set<Integer> set, String str){
return "static: "+list.get(set.iterator().next())+str;
}
public String instanceFun(List<String> list, Set<Integer> set, String str){
return "instance: "+list.get(set.iterator().next())+str;
}
}