iT邦幫忙

2021 iThome 鐵人賽

DAY 10
0
Mobile Development

Kotlin Android 30天,從 0 到 ML (Machine Learning)系列 第 10

Kotlin Android 第10天,從 0 到 ML - Kotlin 與 Java 互動操作

  • 分享至 

  • xImage
  •  

前言:

   雖然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://ithelp.ithome.com.tw/upload/images/20210915/20121643JusY5juSQ7.png

參考:

https://kotlinlang.org/docs/java-interop.html
https://kotlinlang.org/docs/java-to-kotlin-interop.html


上一篇
Kotlin Android 第9天,從 0 到 ML - 介面與抽象類別 / 泛型 / 擴充
下一篇
Kotlin Android 第11天,從 0 到 ML - Kotlin Coroutine
系列文
Kotlin Android 30天,從 0 到 ML (Machine Learning)30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言