fun main(args: Array) {
val alist = arrayListOf (1,2,3,4)val blist = arrayListOf("a","dd", "","e")alist.forEach { it -> println(it)}//这种写法是函数引用,要求类型一致alist.forEach(::println)val vistor=Vistor()val x = vistor::helloprintln(x("mike"))val a = "abc"a.isNotEmpty()复制代码
public inline fun Iterable.filter(predicate: (T) -> Boolean): List
{
return filterTo(ArrayList(), predicate)复制代码
}
//filter需要一个参数, 这个参数是一个函数,函数的类型是 一个参数T,返回Boolean类型的函数//String::isNotEmpty隐含一个参数,就是调用实例对象的本身,真正调用的是 xx.isNotEmpty()blist.filter(String::isNotEmpty)val pdfPrinter = PdfPrinter()blist.forEach(pdfPrinter::printPdf)复制代码
}
class PdfPrinter(){ fun printPdf(any: Any){ println(any) } }
class Vistor { fun hello(name:String){ println("hello $name") } }