雖然kotlin 為android 官方首選語言,新專案大都也是用kotlin ,但是免不了會有Java的專案要維護或修改,這時就變混合雙打,免不了要互相呼叫或繼承。
1.Kotlin 呼叫 Java Class
//Kotlin
var mJavaPlayer = Day10Ironman()
mJavaPlayer.JavaIronPlayer("Kotlin Android 30天,從 0 到 ML")
//Java
public class Day10Ironman {
private String mTitle;
private String mPlayer = "kevin";
private String mStartDay = "9/1";
....
}
public void JavaIronPlayer(String title){
this.mTitle = title;
}
Kotlin 呼叫 Java Class Method
//Kotlin
mJavaPlayer.printPlayerInfoJava()
println("Java return IronPlayer = "+mJavaPlayer.ironPlayer())
//Java
public void printPlayerInfoJava(){
System.out.println("Java Title = "+mTitle);
}
Boolean isFunction()
//Kotlin
println("Java is Complete Iron = "+mJavaPlayer.isCompleteIron)
//Java
public boolean isCompleteIron(){
Calendar endDay = Calendar.getInstance();
endDay.set(2021,Calendar.OCTOBER,5);
Calendar mToday = Calendar.getInstance();
long diffDay = (endDay.getTimeInMillis()-mToday.getTimeInMillis())
/ (60*60*24*1000) ;
System.out.println("Java log 剩下"+diffDay+"天完賽");
if((int)diffDay > 30){
return true;
}else {
return false;
}
}
Getter 與 Setter
//Kotlin
println("開賽時間"+mJavaPlayer.getmStartDay()) // 9/1
mJavaPlayer.setmStartDay("9/6")
println("修改開賽時間"+mJavaPlayer.getmStartDay())
//Java
public String getmStartDay() {
return mStartDay;
}
public void setmStartDay(String mStartDay) {
this.mStartDay = mStartDay;
}
處理 Null Safety
//Kotlin
println(mJavaPlayer.ironPlayer().uppercase(Locale.getDefault())) //kevin -> KEVIN
println(mJavaPlayer.nullPlayer()?.uppercase(Locale.getDefault()) ?: "Noman")
//null -> Noman
//Java
@NotNull
public String ironPlayer(){
return mPlayer;
}
@Nullable
public String nullPlayer(){
return null;
}
-----------------------------------------------------------------------------------
2.Java 呼叫 Kotlin Class
//Java
Day8Activity.ironman javaIronPlayer =
new Day8Activity.ironman("Kotlin Android 30天,從 0 到 ML","kevin");
//Kotlin
open class ironman constructor(titleName: String, playerName: String){
//DAY10 JAVA 呼叫常數
companion object{
val startDay: String = "9/6"
@JvmStatic
fun teamName(): String = "Koltin 愛台灣 2021"
}
var mTitle = titleName
var mPlayer = playerName
open fun printPlayerInfo(){
println("Title: $mTitle , Player: $mPlayer ")
}
}
Class Properties
//Java
System.out.println(javaIronPlayer.getMPlayer());
javaIronPlayer.setMPlayer("KEVIN");
System.out.println(javaIronPlayer.getMPlayer());
//Kotlin
var mPlayer = playerName
Companion Object Properties
//Java
System.out.println(javaIronPlayer.Companion.getStartDay());
//Kotlin
....
companion object{
val startDay: String = "9/6"
....
Companion Object Method
//Java
System.out.println(javaIronPlayer.Companion.teamName());
//Kotlin
companion object{
val startDay: String = "9/6"
@JvmStatic
fun teamName(): String = "Koltin 愛台灣 2021"
}
Top-Level function
//Java
String mktTitle = new Day10KtClass().KtTitle();
System.out.println( mktTitle);
//Kotlin Day10KtClass.kt
class Day10KtClass {
@JvmName("KtTitle")
fun getIronTitle2():String {return "Kotlin Class"}
}
@file:JvmName @JvmName
//Java
System.out.println(KotlinMain.javaCallKt());
//Kotlin Main.kt
@file:JvmName("KotlinMain")
package com.ithome.kotlin30dayml
@JvmName("javaCallKt")
fun kotlinMainprint():String{return "kotlin main Hello World"}
fun main(args: Array<String>){
println("Hello World")
}
@JvmField
//Java
Day9Activity.webSpeaker mSpeaker = new Day9Activity.webSpeaker("Java call Kotlin");
System.out.println(mSpeaker.mTitle);
//Kotlin
class webSpeaker(title:String): seminarSpeakers(title) {
@JvmField
var mTitle = title
override fun showTitle() {
println("abstract Tilte = $mTitle")
}
}
@JvmStatic
//Java
System.out.println(Day8Activity.ironman.teamName());
//Kotlin
open class ironman constructor(titleName: String, playerName: String){
//DAY10 JAVA 呼叫常數
companion object{
val startDay: String = "9/6"
@JvmStatic
fun teamName(): String = "Koltin 愛台灣 2021"
}
var mTitle = titleName
var mPlayer = playerName
open fun printPlayerInfo(){
println("Title: $mTitle , Player: $mPlayer ")
}
}
執行結果:
https://kotlinlang.org/docs/java-interop.html
https://kotlinlang.org/docs/java-to-kotlin-interop.html