简体   繁体   中英

How to instantiate interface in Kotlin?

setOnPlaceSelectedListener takes an interface as argument. In Java, one could make an instance of that interface by overriding all of the methods on the get-go. How to do this in Kotlin?

autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
    @Override
    public void onPlaceSelected(Place place) {
        txtView.setText(place.getName()+","+place.getId());
        Log.i(TAG, "Place: " + place.getName() + ", " + place.getId());
    }

    @Override
    public void onError(Status status) {
        // TODO: Handle the error.
        Log.i(TAG, "An error occurred: " + status);
    }
});

As you can find the oficial documentation ( https://kotlinlang.org/docs/reference/object-declarations.html ) you can create an object declaration:

window.addMouseListener(object : MouseAdapter() {
    override fun mouseClicked(e: MouseEvent) { ... }

    override fun mouseEntered(e: MouseEvent) { ... }
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM