Kotlin 与java 的互操作性

@JvmName("XX")   使用此注解指定编译kotlin类的自定义名字,在java类里可以直接使用该名称。

@file:JvmName("Hero")



//java类里直接使用该名称
Hero.makeProclamation()

@JvmField :给kotlin里的属性添加@JvmField,可以直接在java类里面调用 直接访问该属性。
@JvmField
val spells = listOf("Magic Ms. L", "Lay on Hans")
companion object{
  //@JvmField 让java类访问此伴生对象里的属性
    @JvmField
    val MAX_SPELL_COUNT = 10
   
}
 Spellbook.MAX_SPELL_COUNT

@JvmOverloads :让java类可以调用kotlin类里面重载的函数,否则java调用该函数时会报错。
@JvmOverloads
fun handOverFood(leftHand: String = "berries", rightHand: String = "beef") {
    println("Mmmm... you hand over some delicious $leftHand and $rightHand")
}
//Java要支持,koltin类Hero的函数handOverFood要重载
 Hero.handOverFood("apple");

@JvmStatic :在Java 类里可以直接调用Kotlin 伴生对象里的函数
companion object{
  //@JvmField 让java类访问此伴生对象里的属性
    @JvmField
    val MAX_SPELL_COUNT = 10
    @JvmStatic
    fun getSpellbookGreeting() = println("I am the Great Grimoire!")
}

//在Java类里直接调用Kotlin类Spellbook的伴生对象的属性和方法

Spellbook.MAX_SPELL_COUNT;
Spellbook.getSpellbookGreeting();

  异常:

 kotlin 调java的异常

  val adversary = Jhava()

try {
    adversary.extendHandInFriendship()
} catch (e: Exception) {
    println("Begone, foul beast!")
}

//java类里的异常

public void extendHandInFriendship() throws IOException {

throw new IOException();

}

java调用Kotlin里的异常 :

Kotlin里的异常 需要使用@Throws(IOException::class)注解,才能让java调用此异常方法

//Kotlin里的异常 需要@Throws(IOException::class)注解
@Throws(IOException::class) fun acceptApology() {
 throw IOException() 
}

//Java类里调用Kotlin 的异常

try{
    Hero.acceptApology();
}catch (IOException e){
    System.out.println("Caught!");
}
public class Jhava {

    private int hitPoints = 3232320;

    public static void main(String[] args) {
        System.out.println(Hero.makeProclamation());
        Spellbook spellbook = new Spellbook();
        for (String spell : spellbook.spells) {
            System.out.println(spell);
        }

        //Java要支持,koltin类Hero的函数handOverFood要重载
        Hero.handOverFood("apple");
        //伴生对象
        //Spellbook.Companion.getMAX_SPELL_COUNT();
        System.out.println(Spellbook.MAX_SPELL_COUNT);
        Spellbook.getSpellbookGreeting();

        try {
            //强制在编译期进行处理
            new Jhava().extendHandInFriendship();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try{
            Hero.acceptApology();
        }catch (IOException e){
            System.out.println("Caught!");
        }
   
   }
 // @NotNull 一定不为空
    @NotNull
    public String utterGreeting() {
        return "HELLO";
    }
    // @Nullable   可能为空
    @Nullable
    public String determineFriendshipLevel() {
        return null;
    }

    public int getHitPoints() {
        System.out.println("-------getHitPoints--------");
        return hitPoints;
    }

    public void setHitPoints(int hitPoints) {
        this.hitPoints = hitPoints;
    }

    public void extendHandInFriendship() throws IOException {
        throw new IOException();
    }
}









@file:JvmName("Hero")

import java.io.IOException

fun main() {
    val adversary = Jhava()
    println(adversary.utterGreeting())
    //平台类型
    val level = adversary.determineFriendshipLevel()
    level?.toLowerCase()

    adversary.hitPoints = 3
    println(adversary.hitPoints)

    handOverFood("apple")

    //Spellbook.MAX_SPELL_COUNT

    try {
        adversary.extendHandInFriendship()
    } catch (e: Exception) {
        println("Begone, foul beast!")
    }

       
}

fun makeProclamation() = "Greetings, beast!"

//调用者可以指定英雄左手或右手拿什么食物,或者使用默认的配置——左手拿浆果,右手拿牛肉。
//
@JvmOverloads
fun handOverFood(leftHand: String = "berries", rightHand: String = "beef") {
    println("Mmmm... you hand over some delicious $leftHand and $rightHand")
}

@Throws(IOException::class)
fun acceptApology() {
    throw IOException()
}


}

 

class Spellbook {
    //@JvmField 让java类访问此属性
    @JvmField
    val spells = listOf("Magic Ms. L", "Lay on Hans")

    companion object{
      //@JvmField 让java类访问此伴生对象里的属性
        @JvmField
        val MAX_SPELL_COUNT = 10
        @JvmStatic
        fun getSpellbookGreeting() = println("I am the Great Grimoire!")
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值