Standard functions

First we introduce the standard functions with, run, and apply. If you know javascript, it’s not easy to understand Kotlin’s standard functions width, run, and apply. With, run, and apply are basically the same as with in javascript, with some subtle differences.

with

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

val result = with(obj) {
	// Here is the context of obj
	doSomething() // Call obj's doSomething method without obj.dosomething ()
}
Copy the code

run

The use of the run function is very similar to the use scenario and the with function, with some modifications. The run function cannot be called directly. It needs to be called on the basis of an object. Second, the run value takes a Lambda expression as an argument and provides the context of the calling object in the Lambda expression, again taking the last line of code in the Lambda expression as the return value.

Var result = obj.run {// doSomething(); // call obj doSomething(); // call obj doSomething();

apply

The apply and run functions are essentially the same in usage, except that the apply function does not return the last line of the Lambda expression as an argument. Instead, it returns the object itself.

val result = obj.apply {
// Here is the context of obj
	doSomething() // Call obj's doSomething method without obj.dosomething ()
}
// result == obj
Copy the code

A static method

Define a static method in Java as follows:

public class Util {
	public static void doSome(a) {
		// todo}}// Use static methods
Util.doSome()
Copy the code

Kotlin offers several ways to implement static methods similar to those found in Java

  • The singleton class implements static methods
// Declare a singleton class
object Util {
	fun doSome(a) {
		// todo}}/ / use
Util.doSome()
Copy the code
  • The accompanying class implements static methods

Writing a singleton class makes all the methods in the class behave like static methods. What if we just want some of the methods in the class to behave like static methods? Kotlin provides us with the companion class Companion Object.

class Utl {
	companion obj {
		fun doSome(a) {
		// todo}}}/ / use
Util.doSome()
Copy the code

This keyword actually creates a companion class inside the Util class. Kotlin guarantees that only one companion class object will exist in a class. Calling util.dosome () actually calls the doSome method of the companion class object in Util.

  • Annotations implement static methods

If we really needed to define truly static methods, we could annotate methods in our singleton or companion Object classes with the @jVMStatic annotation, and the Kotlin compiler would compile those methods to be truly static. Note that this comment is usually applied to methods of singletons or associated classes. If it is applied to ordinary methods, it will directly indicate a syntax error.

class Utl {
	companion obj {
		@JvmStatic
		fun doSome(a) {
		// todo}}}/ / use
Util.doSome()
Copy the code
  • Top-level methods 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 as static methods. All top-level methods can be called directly from anywhere, regardless of package name path or instance creation. But if the method is called in Java code, you need to add the filename of the method.

// We created a top-level method in the tool. kt file
// Tool.kt
fun doSome(a) {
	// todo
}

// Used in Java code
public class JavaTest {
	public void invokeStaticFunc() {
		// Call the top-level method in filename + method form
		Tool.doSome()
	}
}
Copy the code