Stay up late to fight Android Kotlin interoperability

👉 "About the author"

As we all know, life is a long process, constantly overcoming difficulties and constantly reflecting on the process of progress. In this process, there will be a lot of questions and thoughts about life, so I decided to share all my thoughts, experiences and stories in order to find resonance!!!
Focus on Android/Unity and various game development skills, as well as various resource sharing (website, tools, materials, source code, games, etc.)
If there is any need to welcome private self, exchange group, so that learning is no longer lonely.

👉 "Premise"

Earlier, we learned the Kotlin language. Strike while the iron is hot. Let's try the application of Kotlin in Android.

If you are a novice, please complete the basics of Android first.

It is recommended to read the Android series written by Xiao Kong before staying up late and try again.

👉 "Practice process"

😜 First acquaintance

I've been saying that Java and Kotlin have interoperability, but how does it reflect? We still use that example to prove it.

For example, in Java, although the entity class generated by Jason is a java file, it can be used directly in Kotlin. Is it OK to use it casually?

Of course not. We always remember that Ktlin has strict NULL rules.

Therefore, it is better to add the identifier [@ Nullable] to your Java entity that is Nullable and [@ NonNull] to your Java entity that is not Nullable, so that when Java is converted to Kotlin, it will be written according to the null rules in Kotlin according to these identifiers.

public class EntityJava {
    //This is null able and will be converted to String in kotlin?
    private @Nullable
    String name;
    //This is non nullable. Kotlin will treat her as a non nullable type
    private @NonNull
    String age;
    //Of course, if you don't know whether it is empty, it is OK by default, but when you call the quantity, you must judge whether it is empty. If you can also judge!! Can I throw an exception when it is empty? Judgment will do
    private int height;
}

😜 Entity prefix

What is an Entity? Some people are called Entity and some people are called Model, which is actually the type generated by Jason.

If the entity class uses Java,

To use in Kotlin, you need to ensure that the prefix of the attribute is get or set or is, otherwise Kotlin cannot normally use this Java entity.

This can be generated automatically. In fact, we don't need to care about it by default. Right click in the class to find Generate, and then find Getter and Setter

Or use the plug-in [GsonFormatPlus] in the plug-in market.

😜 Functions with default values

In Kotlin, the parameters of a function can have default values

open class Entity {
    fun myTest(name: String = "Mr. empty name", age: String){
        Log.e("TAG,", "I output the content ")
}
}

As mentioned above, the function has two parameters. If two parameters are input during the call, both parameters will change. If only one parameter is input during the call, the age represented by the parameter will be changed

var  entity=Entity()
entity.myTest("sesame seeds","999")
entity.myTest("999") //This can go wrong

Some friends wrote that, but why did they make mistakes. Yes, the above code will make mistakes.

Because if the parameters with default values are not always the last when the function is defined, all parameter values must be passed in

open class Entity {
    fun myTest(age: String, name: String = "Mr. empty name"){
        Log.e("TAG,", "I output the content ")
}
}
var  entity=Entity()
entity.myTest("sesame seeds","999")
entity.myTest("999") //That's right

What about multiple parameters with default values? The same answer is to put the parameters with default values after them.

open class Entity {
    fun myTest(age: String, name: String = "Mr. empty name", height: String = "182") {
        Log.e("TAG,", "I output the content ")
}}

Then how should we call it in Java? Just add an annotation @ JvmOverloads to the function

@JvmOverloads
fun myTest(age: String, name: String = "Mr. empty name", height: String = "182") {
    Log.e("TAG,", "I output the content $age$name$height")
}

//The following is the call in Java

Entity entityTest=new Entity();
entityTest.myTest("20 year");
entityTest.myTest("20 year","My name");
entityTest.myTest("20 year","My name","height");

👉 "Others"

📢 Author: Xiaokong and Xiaokong in Xiaozhi
📢 Reprint instructions - be sure to indicate the source: https://zhima.blog.csdn.net/
📢 Welcome to praise 👍 Collection 🌟 Leaving a message. 📝

👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇

Keywords: Java Android kotlin

Added by mick_otoole on Sat, 06 Nov 2021 05:33:32 +0200