科理化是把一个多参数的函数变成多个单参数的函数。
fun log(tag: String, target: OutputStream, message: Any?) {
target.write("[$tag] $message\n".toByteArray())
}
fun log(tag: String)
= fun(target: OutputStream)
= fun(message: Any?)
= target.write("[$tag] $message\n".toByteArray())
每个函数都是一个参数,看起来好清晰哦。上面两个函数真的一样吗?我们调用运行一下看看。
log("Joel", System.out, "访问了首页。")
log("Smith")(System.out)("访问了次页。")
运行的结果是
[Joel] 访问了首页。
[Smith] 访问了次页。
果然是一样的。
那么如果有返回参数怎么办呢?其实很简单的
例如
fun sing(who: String, shijian: String, where: String): String {
return "$shijian ${who} 在 $where"
}
fun sing(who: String)
= fun(shijian: String)
= fun(where: String)
= "$shijian ${who} 在 $where"
只要在最后一个等号写出你要返回的这个参数就好了。
调用如下
println(sing("月亮", "晚上", "天上"))
println(sing("太阳")("白天")("追月亮"))
运行结果是
晚上 月亮 在 天上
白天 太阳 在 追月亮
网友评论