This is the 11th day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021
preface
Kotlin’s built-in functions are available in Kotlin for extensions and easy coding for developers. Can greatly improve the “development efficiency”. Today, I will mainly explain:
- Let the function
- The braking function
- With the function
- The run function
- The apply function
I. Let function
Method of use
Let {it.todo()} // Function 1: use it instead of object to access its public attribute & method Object.let {it.todo()} .todo()}.todo()}.todo() {.todo()Copy the code
Use the sample
// Use Java if(mVar! = null ){ mVar.function1(); mVar.function2(); mVar.function3(); } // use kotlin (no let function) mVar? .function1() mVar? .function2() mVar? .function3() // use kotlin (use let function) // to facilitate the same null processing & determine the scope of the mVar variable mVar? .let { it.function1() it.function2() it.function3() }Copy the code
2. Also function
Functions & Application scenarios
Similar to let, except that the return value is:
- Let function: return value = last line/return expression
- Also function: Returns the value = itself of the object passed in
Use the sample
// let function var result = mvar.let {it.function1() it.function2() it.function3() 999 Var result = mvar.also {it.function1() it.function2() it.function3() 999Copy the code
The with function
role
When calling multiple methods/attributes of the same object, you can directly invoke the method name/attribute without the object name duplication
Application scenarios
Multiple methods/properties of the same object need to be called
Method of use
with(object){ // ... } // Return value = the last line of the function block/return expressionCopy the code
Use the sample
// kotlin val people = people (" Carson ", 25) with(people) {println("my name is $name, I am $age years old") } // Java User peole = new People("carson", 25); String var1 = "my name is " + peole.name + ", I am " + peole.age + " years old"; System.out.println(var1);Copy the code
The run function
Functions & Application scenarios
Combined with the functions of let and with, namely:
- When calling multiple methods/attributes of the same object, you can directly invoke the method name/attribute without the object name duplication
- Defines a variable in a particular scope
- Do a unified short processing
Method of use
object.run{ // ... } // Return value = the last line of the function block/return expressionCopy the code
Use the sample
// kotlin val people = people (" Carson ", 25) people? .run{ println("my name is $name, I am $age years old") } // Java User peole = new People("carson", 25); String var1 = "my name is " + peole.name + ", I am " + peole.age + " years old"; System.out.println(var1);Copy the code
5. Apply function
Functions & Application scenarios
This is similar to the run function, except that the return value is:
- The run function returns the value/expression of the last line
- The apply function returns the object itself passed in
Application scenarios
Object instance initialization requires the assignment of properties in the object & return the object
Use the sample
// run function val people = people (" Carson ", 25) val result = people? .run{ println("my name is $name, I am $age years old") I am $age years old") I am $age years old Run {println("my name is $name, I am $age years old") 999}} val result = people?Copy the code
conclusion
Above are the functions, application scenarios and usage methods of several kotlin built-in functions. The following are briefly summarized:
If you do not understand, you can leave a message, the official website also has a detailed introduction