源代码如下:
interface StringFunc {
String func(String n);
}
public class LambdaAsArgumentsDemo {
//This method has a functional interface as the type of its first parameter.
//这个方法有一个函数式接口,作为它的第一个参数
//Thus,it can be passed a reference to any instance of that interface,
//因此,它可以作为一个引用传给任何那个接口的实例,
//inlcuding the instance created by a lambda expression.
//包括 lambda表达式所创建的实例
//The second parameter specifies the string to operate on.
//第二个参数指定了打算操作的字符串
//自己:这不就是传一个操作方法,再传一个想操作的字符串吗,
//我想做红烧茄子,老王的茄子,就完事了
//我想做红烧鸡翅,李斯的老母鸡,完事儿。
static String stringOp(StringFunc sf,String s){
return sf.func(s);
}
public static void main(String[] args){
String inStr="Lambda add power to Java";
String outStr1;
String outStr2;
System.out.println("Here is the input String:"+inStr);
//lamda uppercase a string is passed to stringOp()
//lamda 转换字符串为大写字母 被传到stringOp()
outStr1=stringOp((str)->str.toUpperCase(),inStr);
outStr2=stringOp((str)->str.toLowerCase(),inStr);
System.out.println("Here is the output String1:"+outStr1);
System.out.println("Here is the output String2:"+outStr2);
//System.out.println("Here is the input String:");
}
}
运行结果如下:
Here is the input String:Lambda add power to Java
Here is the output String1:LAMBDA ADD POWER TO JAVA
Here is the output String2:lambda add power to java
注意:outStr1=stringOp((str)->str.toUpperCase(),inStr);
outStr2=stringOp((str)->str.toLowerCase(),inStr);
这很优雅,不同的操作,对一个字符串,然后又输出了。
打游戏时,可以传入:
大天使的守护,xxx装备
贤者的伪装,xxx装备
我们得到了想要的装备。
这个最外面的函数就叫做:TopSuitProducer(funInterface,xxx装备散件)