Standard Functions and Static Methods for kotlin Quick Start

  • 2021-11-13 01:32:48
  • OfStack

Directory standard function
with
run
apply
Static method
Singleton class implements static method companion class implements static method annotation implements static method top-level method implements static method summary

Standard function

First of all, we introduce the standard functions with, run and apply. If you know javascript, it is not too easy to understand the standard functions width, run and apply of kotlin. The meanings of with in with, run, apply and javascript are basically identical, but there are only one subtle difference.

with

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


val result = with(obj) {
	//  This is obj Context of 
	doSomething() //  Call obj Adj. doSomething Method, without the need for  obj.doSomething()  This form calls 
}

run

The usage of the run function is very similar to the usage scenario and the with function, with minor modifications. The run function cannot be called directly, it needs to be called on the basis of an object; Next, the run function value takes an Lambda expression as a parameter, and provides the context of the calling object in the Lambda expression, and also takes the last line of code in the Lambda expression as the return value.


val result = obj.run {
//  This is obj Context of 
doSomething() //  Call obj Adj. doSomething Method, without the need for  obj.doSomething()  This form calls 
}

apply

The apply function and the run function are basically identical in usage. The only difference is that the apply function does not return the last line in the Lambda expression as a parameter, but returns the object itself.


val result = obj.apply {
//  This is obj Context of 
	doSomething() //  Call obj Adj. doSomething Method, without the need for  obj.doSomething()  This form calls 
}
// result == obj

Static method

Define a static method in java as follows:


public class Util {
	public static void doSome() {
		// todo
	}
}

//  Use static methods 
Util.doSome()

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

Singleton classes implement static methods


//  Declaration 1 Singleton class 
object Util {
	fun doSome() {
		// todo
	}
}
//  Use 
Util.doSome()

Accompanying classes implement static methods

Singleton classes are written in such a way that all methods in the class are called like static methods. What if we just want some methods in the class to be called like static methods? kotlin provides us with the 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, Kotlin guarantees that there will only be 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.

Annotation implements static methods

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


class Utl {
	companion obj {
		@JvmStatic
		fun doSome() {
		// todo
		}
	}
}

//  Use 
Util.doSome()

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 into static methods. All top-level methods can be called directly from anywhere, regardless of the package name path or the creation of an instance. But if this method is called in Java code, you need to add the file name where the method is located.


//  If we are in Tool.kt  Created in the file 1 Top-level methods 
// Tool.kt
fun doSome() {
	// todo
}

//  In java Use in the code 
public class JavaTest {
	public void invokeStaticFunc() {
		//  Filename + Call the top-level method as a method 
		Tool.doSome()
	}
}

Summarize


Related articles: