/** * 返回匹配给定[谓词 predicate 匿名函数]的字符数。 */publicinline fun CharSequence.count(predicate:(Char)->Boolean):Int{varcount=0for(elementinthis)if(predicate(element))++countreturncount} 代码示例 :在下面的代码中 , 传入了 匿名函数 代码语言:javascript 代码运行次数:0 运行 AI代码解释 {le...
本文简单谈下Kotlin中的函数,包括表达式函数体,命名参数,默认参数,顶层函数,扩展函数,局部函数,Lambda表达式,成员引用,with/apply函数等。从例子入手,从一般写法到使用特性进行简化,再到原理解析。 1.表达式函数体 通过下面这个简单的例子看下函数声明相关的概念,函数声明的关键字是fun,嗯,比JS的function还简单。 Kotli...
3: invokestatic #16 // Method kotlin/jvm/internal/Intrinsics.checkParameterIsNotNull:(Ljava/lang/Object;Ljava/lang/String;)V 6: aload_0 7: invokeinterface #22, 1 // InterfaceMethod kotlin/jvm/functions/Function0.invoke:()Ljava/lang/Object; 12: pop 13: return LineNumberTable: line 3: 6 L...
renderLambdaToString(this) } interface FunctionBase<out R> : Function<R> { val arity: Int } public interface Function<out R> public interface Function1<in P1, out R> : Function<R> { public operator fun invoke(p1: P1): R } 这里的Lambda是一个Kotlin内置的一个类,它就是一个Function,...
interface FG { C apply(A a);} public class ComposeFunInJava { public static void main(String[] args) { G g = (s) -> s.length(); F f = (x) -> x % 2 != 0; FG fg = (x) -> f.apply(g.apply(x)); List strings = new ArrayList(); strings.add("a"); strings.add(...
val function:(String)->Unit = { println(it) } runInterface(function) //普通函数,参数是函数式接口对象,传 函数类型对象 也是可以的 factionTypeReplaceInterface(function) } // 高阶函数, 参数是函数类型对象,传 是函数式接口对象 是不可以的。
interfaceRetrofitService{/** * 获取当天详细信息 * @param date 日期 */@GET("calendar/day")funcalenderDay(@Query("date")date:String,@Query("key")key:String):Observable<CalentarDayBean>/** * 获取近期假期 * @param date 日期 */@GET("calendar/month")funcalenderMonth(@Query("date")date:Stri...
public static final <T> Collection<T> filter(Collection<T>, kotlin.jvm.functions.Function1<T, Boolean>); Notice how the predicate is handled by using the Function1 interface? Now, if we call this in Kotlin: sampleCollection.filter { it == 1 } Something similar to the following will be...
interface Platform { val name: String } expect fun getPlatform: Platform Platform是个接口,使用expect关键字来声明getPlatform,再由Android和iOS通过使用actual关键字分别实现: Android: class AndroidPlatform : Platform { override val name: String ="Android${android.os.Build.VERSION.SDK_INT}" ...
fun main() { myFunction(null) } interface Callback { 一个回调接口 fun callback() { } } fun myFunction(Callback: Callback?) { class MyFunctionStateMachine : Callback { 实现回调接口并加上状态 var state = 0 override fun callback() { myFunction(this)//调用本函数 } } val machine = ...