Kotlin knowledge point understanding, kotlin knowledge point understanding

Source: Internet
Author: User

Kotlin knowledge point understanding, kotlin knowledge point understanding
Kotlin

I will not write the items I searched on the Internet. Just write the questions I recently thought.

1 member variable
class Test2(var a: Int) {    val b = a    val c = 3    init {        println(b)    }}

After compilation

public final class Test2 {   private final int b;   private final int c;   private int a;   public final int getB() {      return this.b;   }   public final int getC() {      return this.c;   }   public final int getA() {      return this.a;   }   public final void setA(int var1) {      this.a = var1;   }   public Test2(int a) {      this.a = a;      this.b = this.a;      this.c = 3;      int var2 = this.b;      System.out.println(var2);   }}

It can be seen from the above that the initialization of member variables is all carried out in the constructor, that is, we can directly assign values to the variables through the parameters passed by the constructor.
Just like the above member variablesSame as B =
Of course, this method only applies to Default constructors.

2 inlin Functions

Let's take a look.After inlin is compiled

class Test2() { init { add(1) } inline fun add(a: Int): Int { return a + 1 }}

Use inlin to modify the compiled Image

public final class Test2 { public final int add(int a) { int var10000 = a + 2; var10000 = a + 3; return a + 1; } public Test2() { int a$iv = 1; int var10000 = a$iv + 2; var10000 = a$iv + 3; int var1 = a$iv + 1; }}

Do not use inlin

public final class Test2 { public final int add(int a) { int var10000 = a + 2; var10000 = a + 3; return a + 1; } public Test2() { int var1 = this.add(1); }}

There is no difference in function compilation, but in calling, one is to directly execute the code in the inlin function, and the other is to call the function.

The inlin function writes the code to the called place, instead of the inlin function.

Calling a function involves the function's inbound and outbound stack operations, which is equivalent to using a part of the stack operations. Specifically, Baidu/Google can be used. inlin functions increase the amount of code and improve efficiency, I don't have any cool operations... You can do it yourself ..

3. Extended Functions

We know that we use java to write, extended functions ??, Tool class, okay... but kotlin can be written like this

inline fun  List  .toArray(conver: (T) -> R): Array  { return Array(size, { conver(get(it)) })}   

Is it really cool to configure it with generics?

list.toArray(){ it.name}

In this wayListName fieldArray

But do you need to note that Kotlin is actually able to call each other with java ???? No, at leastReified not allowed

We can also extend functions for some objects to avoid some things, such as the following

@Suppress("DEPRECATION")@ColorIntfun Resources.getColorCompat(@ColorRes id: Int): Int { return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { getColor(id, null) } else { getColor(id) }}

In this way, we can directly call

context.resources.getColorCompat(id)
4. High-Order Functions
fun add(body:()->Unit){ body()}

Kotlin is eventually compiled into java. class. Can a function be passed in java ??? No. What are the higher-order functions in kotlin ??

Only the basic type variables and objects can be passed in java. are higher-order functions encapsulated into an object by the compiler? Let's take a look at the compiled java file.

public final class Test2 { public final void add(@NotNull Function0 body) { Intrinsics.checkParameterIsNotNull(body, "body"); body.invoke(); }}

What do I see ??Function0 ?? Nana ?? Okay. Click in.

package kotlin.jvm.functions/** A function that takes 0 arguments. */public interface Function0  : Function  { /** Invokes the function. */ public operator fun invoke(): R}  

Do you understand it? Don't worry.Out andOperator directly looks like this.

public interface Function0  : Function  { public fun invoke(): R}  

Do you understand? This is an object. All the higher-order functions are encapsulated into objects.Body. invoke () execution function

InThere are many such functions in kotlin. jvm. functions, which are used to convert high-order functions.

Think about how we generally define the Callback method, interface, one or several methods, which is equivalent to this part of the work that the system has done for us. We just need to write a higher-order function.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.