Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Standard functions and static methods for getting started with kotlin

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Today, I will talk to you about the standard functions and static methods for getting started with kotlin. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.

Standard function

First of all, let's introduce the standard functions with, run, apply, if you know javascript, then understand the kotlin standard functions width, run, apply it is not too easy. The meanings of with, run, apply and with in javascript are basically the same, except that they contain some nuances.

With

With it takes two parameters, the first of which can be any type of object, and the second is an Lambda expression. The with function provides the context of the first object in the Lambda expression, and you can use the object's properties or methods directly without the object prefix. The with function uses the last line of code in the Lambda expression as the return value.

Val result = with (obj) {/ / this is the context of obj doSomething () / / calls the doSomething method of obj without the need to call} run in the form of obj.doSomething ()

The use of the run function is very similar to the usage scenario and the with function, with a few changes. The run function cannot be called directly, it needs to be called on the basis of an object; second, the value of the run function takes an Lambda expression as an argument and provides the context of the calling object in the Lambda expression, again with the last line of code in the Lambda expression as the return value.

Val result = obj.run {/ / here is the context of obj doSomething () / / call the doSomething method of obj without calling} apply in the form of obj.doSomething ()

The apply function and the run function are basically the same in usage, except that the apply function does not return the last line in the Lambda expression as an argument, but returns the object itself.

Val result = obj.apply {/ / here is the context of obj doSomething () / / call the doSomething method of obj without calling the static method} / / result = = obj in the form of obj.doSomething ()

Define a static method in java as follows:

Public class Util {public static void doSome () {/ / todo}} / / use the static method Util.doSome ()

Kotlin provides several ways to implement static methods similar to those in java.

Singleton class implements static method / / declare a singleton class object Util {fun doSome () {/ / todo}} / / use Util.doSome () concomitant class to implement static method

The writing of the singleton class will make all the methods in the class become similar to static method invocation. What if we just want some methods in the class to become static method invocation? Kotlin provides us with companion class companion object.

Class Utl {companion obj {fun doSome () {/ / todo} / / use Util.doSome ()

This keyword actually creates a companion class inside the Util class, and Kotlin ensures that there is only one companion class object in a class, and calling Util.doSome () is actually calling the doSome method of the companion class object in the Util class.

Annotations implement static methods

If we really need to define real static methods, we can annotate the methods in the singleton class or companion object accompanying class with @ JvmStatic, and the kotlin compiler compiles those methods into real static methods. Note that this comment is generally added to the methods of singleton or concomitant classes, and if added to ordinary methods, it will directly prompt for syntax errors.

Class Utl {companion obj {@ JvmStatic fun doSome () {/ / todo} / / use the Util.doSome () top-level method to implement static methods

Top-level methods refer to methods that are not defined in any class, such as the main () method we wrote. The kotlin compiler compiles all top-level methods into static methods. All top-level methods can be called directly from anywhere, regardless of the package name path or creating an instance. But if this method is called in the Java code, you need to add the file name where the method is located.

/ / for example, we have created a top-level method / / Tool.ktfun doSome () {/ / todo} / / call the top-level method Tool.doSome ()} in the java code using public class JavaTest {public void invokeStaticFunc () {/ / filename + method form. Do you have any further understanding of the standard functions and static methods for getting started with kotlin? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report