preface

Recently, I watched "No questions west east" and had a little feeling about it. I strongly believe that thinking about life is not a waste of time, and many great people often think about the meaning of life, the meaning of life. How to be true to yourself is precious. Also please ignore all English punctuation marks!! I'm lazy, too tired to switch. I am tired of working overtime for a long time. I have to continue after teasingCopy the code

1) Use of lambda expressions

Jdk1.8 can now use lambda expressions. In Android development, using lambda expressions can eliminate the need to write anonymous inner classes, the simplest example being setting click events on a View. Koltin’s lambda expressions, with more futures, allow elementary students to perform:

button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { action(); }});Copy the code

Lambda expressions can be omitted like this

button.setOnClickListener{  action() }
Copy the code

Maybe some students who are not familiar with lambda expressions are more confused, why {} wrap is a lambda expression, can save writing anonymous inner class. Now I will give you an analysis;

Full syntax for lambda expressions: eg:

1.
val sum={a:Int,b:Int -> a+b }
Copy the code

If we leave the optional notation, the formula looks like this:

2.
val sum:(Int ,Int) ->Int ={ a,b -> a+b}
Copy the code

Sum is a function type that takes two ints and returns an Int. The type of the function is {a,b -> a+b}.

Kotlin has a feature that when the return type of a function can be inferred, the type can be omitted. That is, by omitting the function declaration, Equation 2 can be reduced to equation 1.

With that in mind, let’s go back and analyze it

button.setOnClickListener{ action() }
Copy the code

The origin of the.

Button’s setOnclickListener accepts an OnviewClick object o and calls back to o.onClick(View V). Then its input parameter can be written as:

button.setOnClickListener(o:(view:View) -> Unit)
Copy the code

Pass in the concrete lambda function object:

button.setOnClickListener({view -> action()})
Copy the code

In kotlin, when a function has only one argument, the argument can be omitted:

button.setOnClickListener({action()})
Copy the code

In kotlin, when a function argument is the last argument to the function, it can be specified after (), so it can be expressed as:

button.setOnClickListener(){action()}
Copy the code

==> In Kotlin, when the function parameter is the only parameter of the function, () can be omitted, so it can be expressed as:

button.setOnClickListener{action()}
Copy the code

This is what I wrote at the beginning of the article; I don’t know if you see what’s going on here?

I don’t know if you’ve ever thought about it, but if a function argument has only one argument, and I omit the argument, what if I need to use that argument in the function body? One might say, well, I won’t omit it. This is true, explicitly declaring an input parameter. In the function body, we can use the parameter directly, but in Kotlin, we implicitly declare its name for us :”it”; Therefore, we need to set the text of the current view as follows:

button.setOnClickListener{ action()
 it.text="google Jiang"
}
Copy the code

The “it” here means view;

2) Extension functions

The power of Kotlin is the extension functions it supports. In Java, we add a method to a class and in general, we need to inherit that class and add a method; In Kotlin you can do this without:

public class Foo{
 public void sing(String song){
   	Log.d(TAG,"Foo sing:"+song)
	}
}
Copy the code

To add a talk() method to the class, kotlin would write:

Foo.talk(talk:String ){
 Log.d(TAG,"Foo talk: $talk")
}

Foo().apply{sing("Grow to like you.")}.talk("lovely")
Copy the code

Result Log output:

Foo Sing: Grow to like you

Foo talk:lovely

Kotlin has a standard function library standardkt. class that defines many extension functions apply,run,let, and so on….. ; We can take a look at the apply source code to show you the meaning of the extension function:

@1
public inline fun <T> T.apply(block: T.() -> Unit): T { block(); return this }
Copy the code

Let’s analyze the apply function in detail: Inline means inline function, so let’s ignore that if we don’t know what that means, I’ll say it again when I have time, T is a generic type, and T.apply indicates that T extends an apply function on class T, whose input parameter block is a function object with no return value, and the return value of apply is of type T. The function specifically executes the input parameter block() and returns itself, that is,T;

T.()->Unit = T.()->Unit

So the block argument is T.(), which means that the input argument is a function of T; The return value of block is void; For any object T, call t. apply{}, pass fun1 to T, execute fun1, and return T itself.

The end of the

Pure hand play, analysis to this, the article is relatively short (PS can force so much is not easy); Please also point out that I am still a primary school student. Try a wave of knowledge realization, people reward (just for fun, forgive my impudence lol)Copy the code