Java9 编程参考官方大全(第10版)第15章 lambda表达式:15.4作为参数传递lambda表达式

源代码如下:

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装备散件)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值