Kotlin implements the simplified call of multi function interface

For an interface that declares multiple methods, we sometimes focus on only a few key methods and do not need to implement all of them. However, due to the syntax limitation of interface call, we have to show that the declaration implements the methods we don't care about in the code. There are also ways to simplify interface calls in Java. For example, viewpager in Android monitors the interface pagechangelistener during page switching. The official provides a simple class: viewpager.simpleonpagechangelistener to simplify calls.

For kotlin, you can use java to realize the simplified call of multi function interface, but you need to use the object keyword and there will still be a lot of code. Here, because kotlin's syntax is more flexible, there should be a unique way to implement this call, making less code and more tension.

For a small example, first briefly introduce the simplified call of multi-function interface similar to Java. First, show what is the simplified interface call, and second, make a comparison. All the examples here are written by kotlin.

1. Declare the interface callback and call the class worker

2. Directly call the interface and use the object keyword. At this time, it is assumed that you do not care about the success method, but the declaration must be displayed.

3. Java style simplified call, declaration of simplified class simcallback

4. The Java style simplifies the display of calls, and there is no need to display declarations for onsuccess that you don't care about

The above calls with strong Java flavor not only have mixed code styles, but also have redundant code. Here's the point. How can kotlin overcome the above two points. Look at the example.

1. It is also a callback interface, but its simplified class needs to be done in kotlin style:

Two function type variables are declared_ Onsucess and_ Onfailure is used to internally decompose the original large interface callback. Then the corresponding methods success and fail are declared to assign values to the variable small interface. Next is the processing of the original interface method, such as the operation of the original interface method onsuccess (STR: String)_ OnSucess?. Invoke (STR) is used to transfer interface callback results. The purpose of doing this is to prepare for future simplified calls.

2. Calling class worker also needs to be handled. The code is basically the same as the original ones, but the following methods are added. Note that if it is not for compatibility with the Java method, the original setcallback can no longer be declared and can be assigned directly. Therefore, the new method can be regarded as a replacement of the original setcallback method.

Note the name. This method accepts a function parameter. The function of the method is to internally generate a simplified interface object simplecallback, and then register the calling class with the interface. Then execute the passed function parameters. Why? It is to use the flexible closure {} in kotlin syntax. In addition, with powerful function extension syntax, it is easy to add this method without changing the original class.

3. Finally, let's look at the calling method:

Finally, it can be seen that when calling the multi-function interface callback, you do not need to declare the interface, but directly declare the method you want to use in the closure, and then execute the operation in the corresponding method closure. The style is completely kotlin, and it is particularly easy to use.

After sharing, I hope it will be helpful to readers.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>