How to create an instance of anonymous interface in Kotlin?

前端 未结 4 1166
北海茫月
北海茫月 2020-12-02 09:49

I have a third party Java library which an object with interface like this:

public interface Handler {
  void call(C context) throws Exception;
}
         


        
4条回答
  •  误落风尘
    2020-12-02 10:24

    The simplest answer probably is the Kotlin's lambda:

    val handler = Handler {
      println("Hello world")
    }
    
    handler.call(myContext) // Prints "Hello world"
    

提交回复
热议问题