高阶函数是一种特殊的函数,它接受函数作为参数,或者返回一个函数
import java.awt.geom.Area fun main(arg: Array<String>) { val mobilePhoto = mobilePhoto() processProduct(mobilePhoto,::mobliePhoneArea) print(mobilePhoto) } interface product{ var area:String fun sell(name:String) } class mobilePhoto:product{ override var area: String ="" override fun sell(name: String) { println("卖$name") } override fun toString(): String { return "mobilePhoto(area='$area')" } } fun mobliePhoneArea(name:String):String{ return "$name 美国" } fun processProduct(product: product,area: (name :String)->String):product{ product.area=area("iphoto") return product }
输出:
mobilePhoto(area='iphoto 美国')