Coexistence of Kotlin and Java, calling kotlin code in -Java

Call of kotlin data class Call of object simple profit mode class in Kotlin Calling file class in Kotlin Flexible transf...
Call of kotlin data class
Call of object simple profit mode class in Kotlin
Calling file class in Kotlin
Call of jvm annotation, flexible transfer of parameters
String string null call

Call of kotlin data class

kotlin Code:

package com.tianfu.kotlin /** * age Member annotations cannot have get and set methods. They can only assign values directly and cannot declare private */ data class Person(var name:String,@JvmField var age:Int)

java code

import com.tianfu.kotlin.Person; public class PersonMain { public static void main(String[] args) { Person person=new Person("Andy",27); System.out.println(person.getName()+" is "+person.age);//There are no get and set methods for age here person.setName("Boby"); person.age=37; System.out.println(person.getName()+" is "+person.age); // person.setName(null); you can't leave it empty here. An error will be reported when running } }

result:

Call of object simple profit mode class in Kotlin

kotlin Code:

package com.tianfu.kotlin object Singleton { fun syhello(){ println("The Ming moon in Qin Dynasty") } }

java code

import com.tianfu.kotlin.Singleton; public class AccessToObject { public static void main(String[] args) { Singleton.INSTANCE.syhello(); } }

Calling file class in Kotlin

kotlin code

package com.tianfu.kotlin /** * kotlinFile The File will be compiled into the corresponding class */ fun printHello(){ println("Only by crossing the sea can we show the true qualities of a hero") } fun main(args: Array<String>) { printHello() }

Calling Java code

import com.tianfu.kotlin.PackageKt; public class CallPackageMethod { public static void main(String[] args) { PackageKt.printHello();//Compile the kotlinfile of package into class, then adjust the method. } }

Call of jvm annotation, flexible transfer of parameters

kotlin annotation code

package com.tianfu.kotlin class OverLoads { @JvmOverloads//If you add this annotation, the method parameters will have default values. Otherwise, all three parameters will be passed in java fun overloaded(a:Int,b:Int=0,c:Int=1){ println("$a,$b,$c") } }

Java call code

import com.tianfu.kotlin.OverLoads; public class AccessToOverLoads { public static void main(String[] args) { OverLoads overLoads=new OverLoads(); overLoads.overloaded(10,11,12);//Methods with three parameters overLoads.overloaded(13);//b. C use the default value overLoads.overloaded(12,13);//c use the default value } }

String string null call

kotlin code

package com.tianfu.kotlin fun String.notEmpty():Boolean{//There are no parameters return this !="" }

Java code to achieve null, direct transfer parameters

import com.tianfu.kotlin.ExtensionMethodKt; public class CallExtensionMethod { public static void main(String[] args) { System.out.println(ExtensionMethodKt.notEmpty("Blue sky and misty rain")); } // Results: true }

8 July 2020, 12:02 | Views: 8119

Add new comment

For adding a comment, please log in
or create account

0 comments